我想知道如何集成这两个宝石(设计+强参数),因为强的params可能会被添加到4.0中的rails core
欢迎任何帮助谢谢
我正在向foo_ids我的控制器发送一系列关联ID .为了允许一组值,我使用:
params.permit(foo_ids: [])
Run Code Online (Sandbox Code Playgroud)
现在,问题是如果我发送一个空数组foo_ids,则忽略该参数.不foos应该将all 作为空数组清除,而是保持关联,因为foo_ids不允许.
这可能是因为在rails中将空数组转换为nil,并且由于强参数正在查找标量值数组而不是单个标量值,因此忽略该nil值.
谁能提出一个解决这个问题的好方法?谢谢!
附加信息
在更新控制器操作中,我需要能够处理两种情况.我需要能够设置foo_ids为空数组.foo_ids如果我只想更新另一个字段,我还需要能够忽略.foo_ids如果nil对第二种情况不起作用,则设置为空数组.
我正在玩Rails 4.x beta并尝试使用carrierwave获取嵌套属性.不确定我正在做的是正确的方向.搜索后,最后查看导轨源和强参数,我发现了下面的注释.
Run Code Online (Sandbox Code Playgroud)# Note that if you use +permit+ in a key that points to a hash, # it won't allow all the hash. You also need to specify which # attributes inside the hash should be whitelisted.
所以它说你必须在has中指定每一个单独的属性,我尝试了以下内容:
帕拉姆的例子:
{"utf8"=>"?",
"authenticity_token"=>"Tm54+v9DYdBtWJ7qPERWzdEBkWnDQfuAQrfT9UE8VD=",
"screenshot"=>{
"title"=>"afs",
"assets_attributes"=>{
"0"=>{
"filename"=>#<ActionDispatch::Http::UploadedFile:0x00000004edbe40
@tempfile=#<File:/tmp/RackMultipart20130123-18328-navggd>,
@original_filename="EK000005.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"screenshot[assets_attributes][0][filename]\"; filename=\"EK000005.JPG\"\r\nContent-Type: image/jpeg\r\n">
}
}
},
"commit"=>"Create Screenshot"}
Run Code Online (Sandbox Code Playgroud)
调节器
def screenshot_params
params.require(:screenshot).permit(:title,
:assets_attributes => [:filename => [:@tempfile,:@original_filename,:@content_type,:@headers]
Run Code Online (Sandbox Code Playgroud)
以上不是"工作"(它不触发载波)但是当我使用我发现的标准嵌套示例时,我不再收到错误(未经许可的参数:文件名):
def screenshot_params
params.require(:screenshot).permit(:title, assets_attributes: :filename)
Run Code Online (Sandbox Code Playgroud)
如果有人能提供帮助就会很棒.我无法找到嵌套了一个指向哈希的键的示例.
我的情况很像Railscast 196-197:嵌套模型表格.但是,我遇到了这种方法和强参数之间的冲突.我无法想出一个很好的方法来填充子对象上的父记录id字段,因为我不希望通过表单分配它(以防止用户将子记录与他们不拥有的父记录相关联) ).我有一个解决方案(见下面的代码),但这似乎是Rails可能有一个聪明,简单的方法为我做的事情.
这是代码......
有一个父对象(称之为Survey)有has_many子对象(称之为问题):
# app/models/survey.rb
class Survey
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions
end
# app/models/question.rb
class Question
validates :survey_id, :presence => true
belongs_to :survey
end
Run Code Online (Sandbox Code Playgroud)
有一个表单允许用户同时创建调查和该调查的问题(为简单起见,下面的代码将调查视为只有问题):
# app/views/surveys/edit.html.erb
<%= form_for @survey do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.fields_for :questions do |builder| %>
<%= builder.label :content, "Question" %>
<%= builder.text_area :content, :rows => 3 %><br />
<% end %>
<%= f.submit "Submit" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
问题是控制器.我想通过强参数保护问题记录中的survey_id字段,但这样做的问题不会通过验证,因为survey_id是必填字段.
# app/controllers/surveys_controller.rb …Run Code Online (Sandbox Code Playgroud) 我无法获得has_many:通过使用Rails 4强大参数的关联.我有一个名为的模型Checkout,我需要Employee在新的结帐表单中从模型中选择一个人.结账和员工通过Employment模型关联.
我尝试创建新的结帐时收到此错误:
NoMethodError in CheckoutsController#create
undefined method `employee' for #<Checkout:0x007ff4f8d07f88>
Run Code Online (Sandbox Code Playgroud)
似乎我的创建操作,结帐参数或我的新结帐表单都有问题.这是创建动作:
def create
@user = current_user
@checkout = @user.checkouts.build(checkout_params)
respond_to do |format|
if @checkout.save
format.html { redirect_to @checkout, notice: 'Checkout was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的结帐参数:
def checkout_params
params.require(:checkout).permit(:job, :employee_ids, :shift, :date, :hours, :sales, :tips, :owed, :collected, :notes)
end
Run Code Online (Sandbox Code Playgroud)
我的新结账表格:
<div class="field">
<%= f.label :employee %><br>
<%= f.collection_select(:employee_ids, Employee.all.collect, :id, :full_name, {:prompt => "Please …Run Code Online (Sandbox Code Playgroud) 在强参数中是否有一种方法可以允许nested_attributes模型的所有属性?这是一个示例代码.
class Lever < ActiveRecord::Base
has_one :lever_benefit
accepts_nested_attributes_for :lever_benefit
end
class LeverBenefit < ActiveRecord::Base
# == Schema Information
# id :integer not null, primary key
# lever_id :integer
# explanation :text
end
Run Code Online (Sandbox Code Playgroud)
对于杠杆强参数,我正在写这个
def lever
params.require(:lever).permit(:name,:lever_benefit_attributes => [:lever_id, :explanation])
end
Run Code Online (Sandbox Code Playgroud)
有没有办法可以编写嵌套属性来允许所有属性而不显式赋予属性名称lever_id和explanation?
注意:请不要与此问题混淆,permit!或者permit(:all)这是为了允许所有嵌套属性
ruby ruby-on-rails ruby-on-rails-3.2 strong-parameters ruby-on-rails-4
我有一个通过关联构建新模型的create方法,如果POST请求中没有params,我希望它返回带有一些文本的400响应.但是,我收到一个错误.
这是在Rails 4.0.2中
控制器方法:
def create
@cast_profile = current_user.build_cast_profile(cast_profile_params)
if @cast_profile.save
redirect_to cast_profile_path
else
render :edit
end
end
def cast_profile_params
params.require(:cast_profile).permit(:name, :email, :public)
end
Run Code Online (Sandbox Code Playgroud)
如果我通过params它很好,但我正在尝试测试错误的请求方案.这是错误:
ActionController::ParameterMissing: param not found: cast_profile
Run Code Online (Sandbox Code Playgroud)
我可以明确地拯救它,但我认为强大的参数应该自动完成.
我在我的控制器中有这个:
params.require(:item).permit!
Run Code Online (Sandbox Code Playgroud)
让我们假设这个rspec规范:
put :update, id: @item.id, item: { name: "new name" }
Run Code Online (Sandbox Code Playgroud)
它按预期工作,没有错误.但是,如果我使用这个:
put :update, id: @item.id, item: nil
Run Code Online (Sandbox Code Playgroud)
我得到ActionController::ParameterMissing了我不想得到的东西.它与我用于其他操作的控制器宏有关,通过它我无法控制被发送的参数(宏检查用户凭据,因此我并不真正关心实际测试#update操作,而是我只测试before_filters for它).
所以我的问题是:如果它存在的话,如何在其中制作params[:item]可选但仍然过滤的属性?
我遇到了载波和轨道4强参数的问题.我有一个非常简单的模型,带有载波上传按钮.如果有人在没有选择要上传的文件的情况下提交上传表单,我想显示错误消息.
现在,我收到param not found:photo此消息的错误:
# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:image)
end
Run Code Online (Sandbox Code Playgroud)
发生此错误是因为Rails 4的强参数要求提供图像参数以提交表单,但由于用户未选择图像,因此不存在.
我无法找到解决此问题的方法,并将其重定向到相同的操作并显示错误消息.
有没有办法用强参数做到这一点?
当我尝试使用没有选择照片的表单时,这是开发日志:https: //gist.github.com/leemcalilly/09b6166ce1af6ca6c833
当我选择照片并成功上传时,这是开发日志:https: //gist.github.com/leemcalilly/1f1e584f56aef15e7af1
其他相关文件:*models/photo.rb - https://gist.github.com/leemcalilly/95c54a5df4e4ee6518da *controllers/photos_controller.rb - https://gist.github.com/leemcalilly/c0723e6dc5478b0f914d *uploaders/image_uploader.rb - https://gist.github.com/leemcalilly/5e43f6524734991773ae *views/photos/index.html.erb - https://gist.github.com/leemcalilly/a8c4c808e5e8a802933b *views/photos/_form.html.erb - https ://gist.github.com/leemcalilly/cd0fd518c1b47d9bfb62 *initializers/carrierwaver.rb - https://gist.github.com/leemcalilly/49e04fa1dda891dd108b
我正在使用Rails 4,我不知道在没有必要参数的情况下使用强参数的最佳方法是什么.那就是我做的:
def create
device = Device.new(device_params)
.................
end
private
def device_params
if params[:device]
params.require(:device).permit(:notification_token)
else
{}
end
end
Run Code Online (Sandbox Code Playgroud)
我的设备型号不验证任何内容.我知道我也可以这样做:
device = Device.new
device.notification_token = params[:device][:notification_token] if params[:device] && params[:device][:notification_token]
Run Code Online (Sandbox Code Playgroud)
是否有任何惯例或正确的方法来做到这一点?