I cannot figure out what I am doing wrong. I have two models:
class Product < ActiveRecord::Base
has_one :review, dependent: :destroy
accepts_nested_attributes_for :review, allow_destroy: true
end
class Review < ActiveRecord::Base
belongs_to :product
end
They have a has_one relationship. The database has the product_id column in the reviews table.
My controller is straight forward on the new (@product = Product.new) and edit action has nothing. Here are my strong params:
def product_params
params.require(:product).permit(:name, ..., review_attributes: [:id, :rating, :text, :author, :name] )
end
My form is as follows:
<%= form_for(@product, :html => {multipart: true, :class => "form-horizontal"}) do |f| %>
...
<%= f.fields_for :review do |ff| %>
<%= ff.hidden_field :author, :value => 'Yes' %>
<%= ff.label :rating, "Enter a Rating" %>
<%= ff.number_field :rating, class: "form-control input-md", min: 0, max: 5, step: 0.5 %>
<%= ff.label :name, "Title of Review" %>
<%= ff.text_field :name, class: "form-control input-md" %>
<%= ff.label :text, "Review Description" %>
<%= ff.text_area :text, class: "form-control" %>
<% end %>
<%= f.submit "Create Product", :class => 'btn btn-default btn-lg' %>
<% end %>
I cannot figure out why the nested form does not appear when I have the accepts_nested_attributes in the model, whether or not I need that accepts_nested_attributes, and why I get an error saying "unpermitted parameters: review" when I do not have the accepts_nested_attributes and submit the form. Any help is greatly appreciated.
In the controller, try to build the review object in the method that is rendering that form...
def new
@product = Product.new
@product.build_review
end