59802

New model insert overwriting others

Question:

I have a rails application where I have two models called <em>Column</em> and <em>Row</em>:

Column:

has_many :rows accepts_nested_attributes_for :rows, :reject_if => lambda { |b| b[:data].blank? }

Row:

belongs_to :column

And the form:

<%= form_for [@table, @row] do |f| %> <% @columns.each do |column| %> <%= f.label :data %><br> <%= f.text_area :data %> <% end %> <%= f.submit %> <% end %>

and my create action in the controller:

@row = Row.new(row_params) if @row.save redirect_to table_rows_path, notice: 'row was successfully created.' else render action: 'new' end

and my new action:

@columns = Column.where(:table_id => @table.id) @row = Row.new(id: @table.id)

So I have two problems. The first is if I have say two columns, so there will be two textfields on the new row page, and I enter "Test" in the first text field and "Another Test" in the second. The only thing that is getting saved is the second. The first one saves "Another Test" instead of "Test".

Also, how can I get the row (which belongs to a column) to save a column_id inside each row?

Thanks for all help!

Answer1:

You are setting the new Row's id to the @table.id. Take that out; you never need to create a new .id. Then use column_id: params[:column_id] to link them up.

Recommend

  • SQL Azure - Substring Searches?
  • Rails ajax form not submitting from within another
  • Rails how to validate file format?
  • Incorrect syntax near 'for' SQL Server
  • undefined method `posts_path' for #
  • ruby on rails TypeError in Users#show - Cannot visit Like
  • I can't create model objects using accepts_nested_attributes_for. It won't create the nest
  • Embedded or referenced relations
  • Rails has_many :through association: save instance into join table
  • Rails CarrierWave versions are not created for some reason
  • Why does PHP appear to evaluate this condition incorrectly?
  • Contact form problem - I do receive messages, but no contents (blank page)
  • How to validate a year I enter in textbox using jquery rule?
  • How to Add Polymorphic Comments to Feed?
  • jquery code not working without breakpoint
  • IE10 strips out hashtag from the URL
  • jQuery: How to AJAXify WordPress Search?
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Button click event not firing in jQuery
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • JSON response opens as a file, but I can't access it with JavaScript
  • req.body is undefined - nodejs
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • SQL merge duplicate rows and join values that are different
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Trying to get generic when generic is not available
  • Can't mass-assign protected attributes when import data from csv file
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal