Reputation: 3410
I have two models: PointPage
and PointPageClass
, which belongs_to :point_page
. When user create PointPage
rails creates several (now there're 6 of them) PointPageClasses
in database. I need to edit all of them on PointPage
edit
page. So, I'm using nested attributes in controller and fields_for
in view.
point_page_form
<%= form_for @point_page, role: 'form' do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
... # some other PointPage fields
<% @point_page.point_page_classes.each do |class| %>
<%= f.fields_for :point_page_classes do |builder| %>
<div class="row">
<div class="col-lg-6">
<%= builder.text_field :distance_price, class: 'form-control' %>
</div>
<div class="col-lg-6">
<%= builder.text_field :show_price, class: 'form-control' %>
</div>
</div>
<% end %>
<% end %>
<%= f.submit 'Save', class: 'btn btn-default' %>
<% end %>
point_page_controller
def update
@point_page = PointPage.find(params[:id])
if @point_page.update_attributes(point_page_params)
respond_to do |format|
format.html { redirect_to new_point_path }
format.js
end
end
end
private
def point_page_params
params.require(:point_page).permit(:name, :url, :article,
point_page_classes_attributes: [:distance_price, :show_price])
end
So, I've added in point_page_controller
PointPageClass
attributes, but when I save the changes, it only saves changes for PointPage
, but not for PointPageClass
(distance_price
and show_price
).
Thanks for any help!
Upvotes: 0
Views: 39
Reputation: 33542
One of the problem could be not permitting the :id
in the point_page_params
.For the update
to take place you need to permit the :id
.
Try changing your point_page_params
to like this
def point_page_params
params.require(:point_page).permit(:id,:name, :url, :article,point_page_classes_attributes: [:id,:distance_price, :show_price])
end
Upvotes: 1