Rails 4 nested attributes multiple records insert instead updating


I'm stuck and i don't know why it is not working right. I have a model
lesson wich has many sublessons. When i update the lesson rails update
properly the lesson attributes but is creating another sublessons
records instead of just updating it.
Here is my code
View Form:
<div style="padding:13px">
<%= form_for @lesson do |f| %>
<%= f.label :lesson_name, "Lesson title" %></br>
<%= f.text_field :lesson_name, class: "form-control" %></br>
<%= f.label :lesson_icon, "Choise icon" %>
<%= f.select "lesson_icon", options_for_select([ "ico03",
"ico04","ico05","ico06" ])%></br></br>
<div>
<%= f.fields_for :sublessons do |sublesson_a| %>
<%= sublesson_a.label :sublesson_name, "Subtitle 1" %></br>
<%= sublesson_a.text_field :sublesson_name, class:
"form-control" %></br>
<%= sublesson_a.label :sublesson_content, "Content" %></br>
<%=sublesson_a.text_area 'sublesson_content', rows: 3, class:
'form-control'%></br>
<%= sublesson_a.label :sublesson_video_link, "Video link"
%><br>
<%= sublesson_a.text_field :sublesson_video_link, class:
"form-control" %></br>
<%end%>
</div>
</div>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
lesson model
class Lesson < ActiveRecord::Base
has_many :sublessons, :dependent => :destroy
accepts_nested_attributes_for :sublessons, :reject_if => :all_blank
end
sublesson model
class Sublesson < ActiveRecord::Base
belongs_to :lesson, :foreign_key => "lesson_id"
validates :sublesson_name, presence: true
validates :sublesson_content ,presence: true
end
lesson controller
def update
@lesson = Lesson.find(params[:id])
@sublessons=@lesson.sublessons.all
if @lesson.update(lesson_params)
flash[:notice] = "Lesson updated"
redirect_to lessons_show_url
else
flash[:notice] = "Error"
redirect_to lessons_show_url
end
end
def edit
@lesson = Lesson.find(params[:id])
@sublessons=@lesson.sublessons.all
end
def lesson_params
params.require(:lesson).permit(:lesson_name, :lesson_icon,
sublessons_attributes:[:sublesson_id, :sublesson_name,
:sublesson_content, :sublesson_video_link])
end

Hi Marius,

Can you try adding the :id attribute to the :sublessons_attributes array in lesson_params? You need to permit the id attribute.

Thanks.

Thanks Steve, but now is give me error message...i write :id instead of sublesson_id ...This :id where do he come from from?

Thanks!

Marius… not instead of :sublesson_id. You need to add the :id attribute in addition to the other nested attributes.

Btw, :id is generated as a hidden field by fields_for. Check here: fields_for (ActionView::Helpers::FormHelper) - APIdock

Thanks!

Thanks Steve it's working... i have this hidden fields for adding anothers sublessons and it's seems like it's generating some nasty ids...if i want to create and update same time i have to make another method i think....i 'm new to this business i'm struggle to learn the conceps...i came from php wordpress and there are diferent stuff <%= f.fields_for :sublessons do |sublesson_aa| %>            <div class="add_new_subtitle delete-subtitle-<%=hh%>" style="padding-bottom: 25px;">             <%= sublesson_aa.label :sublesson_name, "Subtitle 1" %></br>             <%= sublesson_aa.text_field :sublesson_name, class: "form-control delete-sublesson-field-#{hh}", :value => ""%></br>             <%= sublesson_aa.label :sublesson_content, "Content" %></br>             <%=sublesson_aa.text_area 'sublesson_content', rows: 3, class: "form-control delete-sublesson-content-#{hh}", :value => ""%></br>             <%= sublesson_aa.label :sublesson_video_link, "Video link" %><br>             <%= sublesson_aa.text_field :sublesson_video_link, class: "form-control delete-sublesson-video-#{hh}", :value => "" %></br>             <a style="float:right" class="btn btn-primary" id="remove_sublesson" onclick="remove_sublesson(<%=hh%>)">Delete Sublesson</a>             <p style="display:none"><%= hh=hh+1%></p>           </div>         <%end%>

Thanks!

Maurius. I am not able to understand your second question :slight_smile: Would be nice if you started a new thread with detailed explanation. Btw, there are a lot of resources already that show how to do this sorta stuff. Just do some searching. If you dont find luck then I suggest start a new one :slight_smile:

Thanks man,

I am working on Address book and i face problem regarding how to added dynamic column by each user. one things is each user display its own column after add column how to submit all values inside one form.

view (e.g consider mike is user and its create name,city and address dynamic column how three row inserted into database. )

<h1>Add Field values</h1>

<%= form_for(:detail, :url => {:controller => 'details', :action => 'create'}) do |f| %>

  <% @info.each do |a| %>     <%= f.hidden_field :columns_id, :value =>a.id %>      <p>       <td><%= a.value %></td>       <td><%= f.text_field :column_value %></td>     </p></br>   <% end %>

  <%= f.hidden_field :users_id, :value => current_user.id %>   <p class="button"><%= f.submit %></p>

<% end %>

controller

class DetailsController < ApplicationController   def new       @detail = Detail.new        @info = Column.where(:users_id => current_user.id).all   end

  def create     @detail = Detail.new(user_params)     if @detail.save       redirect_to new_detail_path, :notice => "Added Sucessfully!"      else       render "new"     end   end

private

  def user_params       params.require(:detail).permit(:users_id,:column_value,:columns_id)   end end