大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails 4强参数,包含多个对象和整数键,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<%= simple_fields_for "derps[]",derp do |f| %> <% end %>
然后我在控制器中这样做:
def update @derps = [] @rejects = [] derps_params.each do |key,hash| derp = Derp.find(key) derp.assign_attributes(hash) @rejects << derp unless derp.save end if @rejects.empty? redirect_to @parent,flash: {success: 'Derps were successfully updated.'} else @derps = @rejects render :edit end end
让我们说有两个对象 – 参数通过:
"derps"=>{"1"=>{"attribute"=>"39","another_attribute"=>"serp","a_third_attribute"=>"yerp"},"2"=>{"attribute"=>"30","another_attribute"=>"49",}}
我在Rails 3中工作时没有强大的参数.我正在升级到rails 4,我正在努力解决这个问题 – 我不断得到“未经许可的参数:1,2”
我假设我需要做以下事情:
def mashes_params params.require(:derps).permit( id: []
要么
def mashes_params params.require(:derps).permit( :id,
沿着这条路线的东西,但我已经尝试过每一个我能想到没有运气的方式.
这里有什么想法?
rails c # From within your project directory,short for 'rails console' params = ActionController::Parameters.new( { derps: { 1 => { attribute: 39,another_attribute: "serp" },2 => { attribute: 30,another_attribute: 49 } } } ) params # To make sure that the object looks the same permitted = params.require( :derps ).permit( 1 => [ :attribute,:another_attribute ],2 => [ :attribute,:another_attribute ] ) permitted # To see what you'd get BACk in your controller
希望借助此工具,您将能够调试我的答案所提供的任何内容,而不是试验和错误.
以上是大佬教程为你收集整理的ruby-on-rails – Rails 4强参数,包含多个对象和整数键全部内容,希望文章能够帮你解决ruby-on-rails – Rails 4强参数,包含多个对象和整数键所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。