Unpermitted parameters in a simple rails application -
After the
I started my application using scaffolding:
Rail G scaffolding Category titled G Scaffold Rail Product Category: Reference Title Rake db: Uninstalled
I started the server and created a new category and then I used it to create a new product for this category id
wanted to add 1
but when I checked the log:
started POST "/ answer Adon "to make as HTML parameters 2014-08-01 09:28:16 + 0430 processing ProductsController # on 127.0.0.1: {" UTF8 "= & gt; "✓", "authenticity_token" = & gt; "Dd7tvD / DrXf1SiAnCR6ZUvtwQzVuy39HG4A9dmo7Gms =", "Product" = & gt; {"Category" = & gt; "1", "title" = & gt; "Rail"}, "committed" => "Create product"} unpermitted parameters: Category
and in category_id
is the product contorller allowed parameter:
def product_params Params.require (: Products) .permit (: category_id, Title: End)
Model:
class products & lt; ActiveRecord :: Base is_to: category end class category & lt; ActiveRecord :: Base End
Product form:
& lt;% = form_for (@product). F | & Gt%; & Lt;% if @products.rors.ei? & Gt%; & Lt; Div id = "error_explanation" & gt; & Lt; H2 & gt; & Lt;% = Plural (@producer.rors coat, "error")%> Banned from saving this product: & lt; / H2 & gt; & Lt; Ul & gt; & Lt;% @ product.errors.full_messages.each do | Msg | | & Gt%; & Lt; Li & gt; & Lt;% = msg% & gt; & Lt; / Li & gt; & Lt;% end% & gt; & Lt; / Ul & gt; & Lt; / Div & gt; & Lt;% end% & gt; & Lt; Div class = "field" & gt; & Lt;% = f.label: catagory% & gt; & Lt; Br> & Lt;% = f.text_field: catagory% & gt; & Lt; / Div & gt; & Lt; Div class = "field" & gt; & Lt;% = f.label: Title% & gt; & Lt; Br> & Lt;% = f.text_field: Title% & gt; & Lt; / Div & gt; & Lt; Div class = "verb" & gt; & Lt;% = f.submit% & gt; & Lt; / Div & gt; & Lt;% end% & gt;
Can you help me? Are you doing wrong
In your form_for
tag, you have the category
Instead of the field to category_id
ask for)
<% = f.label: catagory_id%> & Lt; Br> & Lt;% = f.text_field: catagory_id% & gt;
In addition to the answer, the relationship between products
and category
is not appropriate. You are missing from has_many
Your category
should look like this model:
class category & lt; ActiveRecord :: Base is - Medium: End of Products
Comments
Post a Comment