Rails 4 nested attributes not saving
Looks like you're using the wrong form builder object. Change the question section to something like this:
<%= f.fields_for :question do |question_builder| %>
<%= question_builder.label :content %>
<%= question_builder.text_area :content %>
<%= question_builder.fields_for :answer do |answer_builder| %>
<%= answer_builder.label :content, "Answer" %>
<%= answer_builder.text_field :content %>
<% end %>
<% end %>
Originally you were using the f
form builder in the line f.fields_for :answers...
, however given your model code, since it is a Question
that accepts_nested_attributes_for
an Answer
, we simply exchange the form builder object for the one used for the question
section.
Note: I changed the names of the builder objects for clarity.
Update
In order to build a deeply nested relationship like this, you will have to loop through your "built" association (questions
in this case), and invoke build
again on its has_many
association. So going off of gabrielhilal's answer, you would do this:
def new
@survey = Survey.new
@survey.questions.build
@survey.questions.each do |question|
question.answers.build
end
end
Note that in your case, since you are explicitly creating only one question, you can technically do this instead (instead of the loop):
@survey.questions.first.answers.build
Disclaimer: If there is a cleaner, more Rails-y, way to do this, I am sadly unaware of it.
Rails not saving nested attributes
You need to add inverse_of
option to the has_many
method in class Task:
class Task < ApplicationRecord
has_many :task_items, inverse_of: :task
has_many :items, through: :task_items
accepts_nested_attributes_for :task_items, :allow_destroy => true
end
This is due to the when creating a new TaskItem instance, it requires that the Task instance already exists in database to be able to grab the id
fo the Task instance. Using this option, it skips the validation.
You can read this post about inverse_of
option and its use cases.
fields_for
has an option to specify the object which is going to store the information. This combined with building each of the TaskItem from the has_many
collection should ensure that all the relationship are set correctly.
View Code:
<%= form_for @task do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field(:title, {:class => 'form-control'}) %><br>
</p>
<% @items.each do |item| %>
<% task_item = @task.task_items.build %>
<%= f.fields_for :task_items, task_item do |ti| %>
<%= ti.label item.description %>
<%= ti.text_field :cost %>
<%= ti.hidden_field :item_id, value: item.id %>
<% end %>
<% end %>
<p>
<%= f.submit({:class => 'btn btn-primary'}) %>
</p>
<% end %>
Controller Code:
def index
end
def new
@items = Item.all
@task = Task.new
end
def create
@task = Task.new(task_params)
@task.save
redirect_to action: "index"
end
private
def task_params
params.require(:task).permit(:id, :title, task_items_attributes: [:id, :item_id, :cost])
end
Rails nested attribute not saving
that's because your @blog
is not persisted in the db yet, so you won't have the id
.
In your Tag
model, remove :id
from validation.
You should be able to just do Blog.create(blog_params)
Rails should handle the rest for you.
Nested attributes in Ruby on Rails not saving
The problem should be in your fields_for
. It should probably be:
<%= f.fields_for :cue do |ff| %>
Not cues
since it is only one. Then you need to build the cue. This can be done either in controller or in the view directly, for example like this:
<%= f.fields_for :cue, @block.build_cue do |ff| %>
In your block params you then also need to change it to cue_attributes
, and also allow id
.
def block_params
params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cue_attributes => [:id, :cue_code, :cue_type_code, :cue_description, :cue_method_name])
end
You can also read a lot more information here:
http://guides.rubyonrails.org/form_helpers.html#nested-forms
Regarding your second Update:
Your first migration adds a column block_id
of type binary
. It should definitely be integer
instead. That said, you don't even need the first migration at all, because your second migration will handle all of it correctly, if you change blocks
to block
in add_reference
. It should look like this:
class AddBelongsToToCues < ActiveRecord::Migration
def change
add_reference :cues, :block, index: true
add_foreign_key :cues, :blocks
end
end
The add_reference
needs to add a reference to one block
not multiple. This will then create the right column for you.
See also: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference
Ruby on Rails nested attributes not saving into database?
You must add the nested params to your strong params
def todo_params
params.require(:todo).permit(:user_id, :list, items_attributes: [:id, :text, ...])
end
Note about todo_id :
You don't need to add :todo_id
in items_attributes
list, because you already have the TODO as context.
@todo = Todo.new(todo_params)
In the above code, your todo_params will contain some item_attributes linked to @todo
. ie, it's similar to doing
@todo.items.build
It will already create an item with a todo_id
corresponding to @todo.id
Rails 4 - belong_to nested object fields are not saved
As you JSON object showing, you are not using rails fields_for or something on view form. So you don't need this( accepts_nested_attributes_for :appealer ) line in your model. Model should be like this:
class Appeal < ActiveRecord::Base
belongs_to :appealer, :autosave => true
endNext thing, in your current logic,for your appealer_params, change appealer_params method with this:
def appealer_params
params.require(:appealer).permit(:first_name, :last_name)
endCreate action logic for your scenario:
def create
@appeal = Appeal.new(appeal_params)
if appealer_params.present?
appealer = Appealer.create(appealer_params)
@appeal.appealer = appealer
end
if @appeal.save
respond_with @appeal
else
respond_with {:error}
end
end
Nested attributes not being saved in rails form
use the iteration of the first @customer so rails can read properly
i saw this example https://www.sitepoint.com/complex-rails-forms-with-nested-attributes/
<%= f.fields_for :booking do |ff| %>
<%= ff.select :city_id,options_for_select(City.pluck(:name, :id)) %>
i hope it works cross finger :)
Rails nested attributes not displayed in SHOW/not saving parent id
By "parent_id is not saved", do you mean gallery_id
is not being saved to album
?
If yes, my first thought is I think you should change this line of code in your album create method from @album = @gallery.albums.new(album_params)
to @album = @gallery.albums.create(album_params)
. This immediately creates the album with the existing params in album_params
which I hope includes the gallery_id
in it before you proceed with adding the photos to the album.
Secondly, can you please update your question with the logs from the console so that we can confirm what's in the album_params
.
Related Topics
How to Check the Database Type in a Rails Migration
What Exactly "Config.Assets.Debug" Setting Does
Rails 3 Install Error: "Invalid Value for @Cert_Chain"
Ruby: Split String at Character, Counting from the Right Side
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
Reflection on Method Parameters in Ruby
Can You Get Db Username, Pw, Database Name in Rails
Storing Arrays in Database Using Activerecord
How to Convert a Ruby Object to JSON
Ruby Timeouts and System Commands
Missing Host to Link To! Please Provide the :Host Parameter, for Rails 4
Ruby Mocha: Is There an Equivalent to Rspec-Mocks' #And_Call_Original
How to Escape #{ from String Interpolation
Extract All Email Addresses from Some .Txt Documents Using Ruby