由于我正在使用Rspec测试我的控制器是否工作正常,所以我做了以下工作:
在test_rspec.rb中,
before do
params = {
date_begin: "2018-10-01",
date_end: "2018-10-05",
options: {country_code: "US"}
}
get :index, params: params, as: :json
end
it do
expected(response).to have_http_response(200) }
我对Rails完全陌生,我正在玩它。现在,我正在尝试理解Rails 4中引入的strong parameter特性。
class PostsController < ApplicationController
def index
end
def show
end
def create
p = Post.new(create_post_params)
p.save
redirect_to p
end
def new
end
private
def create_post_params
params.requ
我使用Rails 3.2.2,我正在寻找一种与 for hashes ()相反的方法,可能是一种only方法。有什么方法或简单的方法可以让我返回只包含给定键的散列吗?
# Example, 'only' method as opposite to the 'except'
@person.update_attributes(params[:person].only(:name, :surname))
请不要攻击。下面是让我思考这个问题的示例和代码。
我正在学习ruby on rails,正在使用MVC和params[]运算符。我已经在我的rails笔记本的内页上写下了一些校样或者其他的东西。
"So we don't just want to call tweet #1. Tweet.find(1) isn't enough."
===> we need Tweet.find(params[:id])
params[:id] aka params = { :id => 1 }
所以我给自己标记了不同的部分,从散列开始。“1是值,:id是键,
我尝试使用Rails 4 Update_attributes来更新嵌套属性,而不是更新
Class Person <ActiveRecord::Base
has_many :pets
accepts_nested_attributes_for :pets
end
Class Pet < ActiveRecord::Base
belongs_to :person
end
在我的控制器中,我收到的是{id: 23,house_no:'22A',宠物:{name:'jeffy',类型:‘狗’},{name:'sharky',类型:‘