Forum: Ruby on Rails Rails 4 nested attributes multiple records insert instead updating

Acaf56d9ede6b8d67f585e3cf809fa91?d=identicon&s=25 Marius Turcu (Guest)
on 2014-08-19 10:48
(Received via mailing list)
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
D2df321347fb07d4a159792a40bb1374?d=identicon&s=25 Steve Robinson (Guest)
on 2014-08-19 10:55
(Received via mailing list)
On Tuesday, August 19, 2014 1:03:03 PM UTC+5:30, Marius Turcu wrote:
> <%= form_for @lesson do |f| %>
> "form-control" %></br>
> <%= 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.
A551dd246cb624b7fd6a78d5ccee835d?d=identicon&s=25 Turcu Marius (mariusturcu)
on 2014-08-19 12:07
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!
D2df321347fb07d4a159792a40bb1374?d=identicon&s=25 Steve Robinson (Guest)
on 2014-08-19 12:23
(Received via mailing list)
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: http://apidock.com/rails/ActionView/Helpers/FormHe...

Thanks!
A551dd246cb624b7fd6a78d5ccee835d?d=identicon&s=25 Turcu Marius (mariusturcu)
on 2014-08-19 13:04
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!
D2df321347fb07d4a159792a40bb1374?d=identicon&s=25 Steve Robinson (Guest)
on 2014-08-22 10:04
(Received via mailing list)
Maurius. I am not able to understand your second question :) 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 :)

Thanks man,
D48be7570943bcf13f30f5be32f5fc41?d=identicon&s=25 Nikhil Thombare (nikhil_t)
on 2014-12-12 08:38
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. )

<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


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));
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.