nested-attributes

.build can't assign foreign key that doesn't exist yet

为君一笑 提交于 2019-12-13 18:28:44
问题 I have the following code for creating pictures for users in my users controller: def new @user = User.new @pictures = @user.pictures.build({:placement => "front"},{:placement => "profile"}) end However, when I create a new user, it isn't automatically building pictures with placement "front" or "profile." In fact, there's no update whatsoever to pictures. The pictures model has attr_accessible :placement and in picture.rb belongs_to :user and in user.rb has_many :pictures accepts_nested

Nested attributes not saving in database? Does not display values - Ruby on Rails

独自空忆成欢 提交于 2019-12-13 17:15:06
问题 I have a nested attribute and I can display values, but it is not saving into database why? I have a model review and I want to nest attribute comments review migration file class CreateReviews < ActiveRecord::Migration def change create_table :reviews do |t| t.belongs_to :reviewable, polymorphic: true t.timestamps end add_index :reviews, [:reviewable_id, :reviewable_type] end end review model class Review < ActiveRecord::Base attr_accessible :rating, :user_id, :comments_attributes, :service

NoMethodError in Index: How to Define the Nested Attributes?

99封情书 提交于 2019-12-13 08:24:37
问题 NoMethodError in Quantifieds#index undefined method `date_value' for #Quantified:0x007ff3fe7d6c08 How do I define date_value & result_value so it works in the quantifieds index? <!-- Default bootstrap panel contents --> <div id="values" class="panel panel-default"> <div class="panel-heading"><h4><b>AVERAGE</b></h4></div> <!-- Table --> <table> <% @averaged_quantifieds.each do |averaged| %> <% if averaged.user == current_user %> <th class="value"> <%= link_to edit_quantified_path(averaged) do

params.require().permit does not work as expected

不问归期 提交于 2019-12-13 08:14:18
问题 I have this controller class PeopleController < ApplicationController def new @person = Person.new @person.phones.new end # this is the action that gets called by the form def create render text: person_params.inspect # @person = Person.new(person_params) # @person.save # redirect_to people_path end def index @person = Person.all end private def person_params params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ]) end end and this view <%= form_for :person, url:

Rails Nested attributes being duplicated

纵饮孤独 提交于 2019-12-13 07:16:12
问题 This problem has beaten me. I've been at it all weekend but can't figure out what is happening. When I create a new employee object, I also want to create multiple new ee_pay records using accepts_nested_attributes_for :ee_pay in my employee model. Creation of ee_pay records for new employees is dependent on the amount of company_pay objects that exist for the company that the employee belongs to. So in the case below there's 2 company_pay objects -> "Basic" [id:2] and "Time + 1/2" [id:3]

Rails 4: child record ID is saved but not its attributes, using nested form with has_many through relationship

笑着哭i 提交于 2019-12-13 06:53:48
问题 I have 3 models with a has_many through relationship: Food (eg: Chocolate), Sub (Chocolate food substitute), Joint (joint table). Say @food = Food.find(1); The has_many through relationship allows me to do @subs = @food.subs which return all substitutes associated with @food. This work fine, however only the Sub id is saved and not its attributes which are :name and :description as you can see it returned nil when trying to save @food.subs in my create action in my controller: => #

Rails 3.2.13 - Delete Entry From Nested Attributes

梦想的初衷 提交于 2019-12-13 04:29:44
问题 I have a Ruby on Rails 3.2.13 application where I have a collection_select statement. The collection_select statement is in a fields_for statement where I gather selected ids from the collection_select and use them to populate another table. The problem I am having is that the collection_select statement adds a null id entry in the array that stores the collection of selected ids. Here is my code in my view: <%= f.fields_for :media_topics do |media_topic| %> <%= media_topic.label :topic,

Rails 4 - Nested models(2 levels) not saving

馋奶兔 提交于 2019-12-13 02:35:51
问题 Noob question on nested models. I am using Rails 4 and trying to create nested models as below: Survey has many questions Each Question has many answers I am following Rails Casts episode #196 to create a survey, questions and answers in the same form. Surevey and Realted questions get saved but answers don't get saved to the database.(The answers fields however are being displayed right.) I really appreciate your inputs on this. Thanks, Mike surveys_controller.rb def index @surveys = Survey

nested attributes, passing current_user.id to nested model

99封情书 提交于 2019-12-13 02:23:53
问题 I have 3 models: User, Answer and Question. user.rb has_many :questions has_many :answers question.rb has_many :answers belongs_to :user accept_nested_attributes_for :answers answer.rb belongs_to :question belongs_to :user In questions/show.html.erb form_for @question do |f| f.fields_for :answers, @question.answers.build do |builder| builder.text_area, :body end f.submit end Submit calls the questions#update action and, thanks to nested resources, will the new answer be saved in the database.

Rails avoid writing attribute of nested object to log

孤人 提交于 2019-12-13 02:15:06
问题 How can I prevent a certain parameter of a nested relationship in rails from entering the log file - I am writing LARGE files to a column in the db and don't want rails to write that to the log file.. I know of filter_parameter_logging but it doesn't seem to work for nested models - I may just be putting in the wrong spot? 回答1: According to the Rails code this should work even for nested parameter hashes. You can implement the filter_parameters method on your controller to work around your