Unpermitted parameters for Dynamic Forms in Rails 4

后端 未结 1 1461
梦毁少年i
梦毁少年i 2020-12-17 05:08

I\'m new to Rails and built something based on this

http://railscasts.com/episodes/403-dynamic-forms

but I have a problem with storing data in the additional

相关标签:
1条回答
  • 2020-12-17 06:06

    If you want to return a nested hash as a parameter you have to name the keys in the array in permit.

    class ProductsController < ApplicationController
    def new
    @product = Product.new(product_type_id: params[:product_type_id])
    end
    def product_params
    params.require(:product).permit(:name, :price, :product_type_id, {:properties => [:foo, :bar, :id]})
    end
    

    If you are generating the keys dynamically and can't code them into the permit statement then you need to use this style:

    def product_params
      params.require(:product).permit(:name, :price, :product_type_id).tap do |whitelisted|
        whitelisted[:properties] = params[:product][:properties]
      end
    end
    

    It's not the most friendly code for a new user, I just finished the 3 course rails certificate at UW and they never even covered .tap.

    This is not my work, I'm still just understanding the deeper parts of .permit like this. This is the blog entry I used: Strong Parameters by Example

    0 讨论(0)
提交回复
热议问题