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:

<%= form_for @lesson do |f| %> <%= f.label :lesson_name, "Lesson title" %> <%= f.text_field :lesson_name, class: "form-control" %> <%= f.label :lesson_icon, "Choise icon" %> <%= f.select "lesson_icon", options_for_select([ "ico03", "ico04","ico05","ico06" ])%>
<%= f.fields_for :sublessons do |sublesson_a| %>
       <%= sublesson_a.label :sublesson_name, "Subtitle 1" %></br>
       <%= sublesson_a.text_field :sublesson_name, class:

“form-control” %>
<%= sublesson_a.label :sublesson_content, “Content” %>
<%=sublesson_a.text_area ‘sublesson_content’, rows: 3, class:
‘form-control’%>
<%= sublesson_a.label :sublesson_video_link, “Video link”
%>

<%= sublesson_a.text_field :sublesson_video_link, class:
“form-control” %>
<%end%>



<%= f.submit “Submit”, class: “btn btn-primary” %>

<% end %>

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])
@[email protected]
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])
@[email protected]
end

def lesson_params
params.require(:lesson).permit(:lesson_name, :lesson_icon,
sublessons_attributes:[:sublesson_id, :sublesson_name,
:sublesson_content, :sublesson_video_link])
end

On Tuesday, August 19, 2014 1:03:03 PM UTC+5:30, Marius Turcu wrote:

<%= form_for @lesson do |f| %>
“form-control” %>
<%= f.submit “Submit”, class: “btn btn-primary” %>
end
def update

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!

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,

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| %>


<%= sublesson_aa.label :sublesson_name, “Subtitle 1”
%>
<%= sublesson_aa.text_field :sublesson_name, class:
“form-control delete-sublesson-field-#{hh}”, :value => “”%>
<%= sublesson_aa.label :sublesson_content, “Content” %>
<%=sublesson_aa.text_area ‘sublesson_content’, rows: 3,
class: “form-control delete-sublesson-content-#{hh}”, :value =>
“”%>
<%= sublesson_aa.label :sublesson_video_link, “Video link”
%>

<%= sublesson_aa.text_field :sublesson_video_link, class:
“form-control delete-sublesson-video-#{hh}”, :value => “” %>
Delete
Sublesson

<%= hh=hh+1%>



<%end%>

Thanks!

I am working on Address book and i face problem regarding how to added
dynamic column by each user. Each user display its own
column. How to submit all multiple record 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. )

Add Field values

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

<% @info.each do |a| %>
<%= f.hidden_field :columns_id, :value =>a.id %>


<%= a.value %>
<%= f.text_field :column_value %>

<% end %>

<%= f.hidden_field :users_id, :value => current_user.id %>

<%= f.submit %>

<% 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

Database structure

CREATE TABLE “users” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
“email” varchar(255), “password_hash” varchar(255), “password_salt”
varchar(255), “created_at” datetime, “updated_at” datetime);

CREATE TABLE “columns” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
“value” varchar(255), “created_at” datetime, “updated_at” datetime,
users_id integer REFERENCES users (id));

CREATE TABLE “details” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
“users_id” integer, “column_value” varchar(255), “created_at” datetime,
“updated_at” datetime, columns_id integer REFERENCES columns (id));