Up vote 1 down vote favorite 1 share g+ share fb share tw.
I have a model "Issue" and a nested Model "Relationship" In the issue. Rb I have mentioned: has_many :relationships, :dependent => :destroy accepts_nested_attributes_for :relationships, :allow_destroy => true In relationship. Rb I have mentioned: belongs_to :issue Following Ryan Bates Railcast#196 I have the following in my issues_controller: relationship = @issue.relationships.
Build However, I am encountering an error "unknown attribute: relationship" Am I doing something incorrectly here? I do see the Relationships Attributes being passed to the server in the log however, this error does not let the create to be successful. My expertise with rails is beginners level so kindly excuse me if I am asking a question which maybe deemed trivial.
Thanks for the help. EDIT: The relevant Controller code: @relationship = @issue.relationships. Build #@relationship = Relationship.
New(params:relationship) if @relationship. Issue_id = '' @relationship. Issue_id = @issueid end if @relationship.
Cause_id = '' @relationship. Cause_id = @issueid end @relationship. Save redirect_to(:back, :notice => 'New Relationship was created') What I see on the trace: ActiveRecord::UnknownAttributeError in IssuesController#create unknown attribute: relationship Among the Issue parameters, I see the Relationship params being passed as expected: "relationship"=>{"issue_id"=>"100", "cause_id"=>""} ANOTHER UPDATE Posting the form_for code: - form_for Issue.
New do |f| . Field = f. Text_field :description, :class=>"formfield", :id=>"frm_descr" .
Field = f. Hidden_field :wiki_url, :class=>"formfield", :id=>"frm_wiki_url" . Field = f.
Hidden_field :short_url, :class=>"formfield", :id=>"frm_img_url" . Field = f. Hidden_field :title, :class=>"formfield", :id=>"frm_title" = f.
Fields_for :relationship do |builder| = builder. Text_field :issue_id, :class=>"form_field", :id=>"frm_rel_issue_id", :value=>@issue. Id = builder.
Text_field :cause_id, :class=>"form_field", :id=>"frm_rel_cause_id" . Actions = f. Submit 'Create', :class=>"save_button", :name=>"save_issue_rel_button", :id=>"val_collector" ruby-on-rails ruby-on-rails-3 nested-forms railscasts link|improve this question edited Jul 27 '11 at 1:34 asked Jul 27 '11 at 0:48rgoraya447 100% accept rate.
Add your complete controller action please and perhaps provide a stack trace to showing where the error is occurring. – Thilo Jul 27 '11 at 1:06 @Thilo: Edited the question to include relevant controller code and trace. – rgoraya Jul 27 '11 at 1:23 What should be passed to your IssuesController#create method is not "relationship", but a "relationship_attributes" hash.
Are you using fields_for as the screencast shows? – Thilo Jul 27 '11 at 1:28 @Thilo: Just posted another EDIT to include the Form_for code Im using. – rgoraya Jul 27 '11 at 1:32 @Ryan: The rails documentation also points out that the parameters for nested attributes should be passed to server as an attribute hash i.e.
Relationship_attributes => { :blah => 'blah' } in this case. We see this is not happening. How can I correct this to be passed as an attribute hash instead?
– rgoraya Jul 27 '11 at 4:43.
Change this line = f. Fields_for :relationship do |builder| to this: = f. Fields_for :relationships do |builder| Your issue has_many relationships - plural.
That will give you the correct relationships_attributes parameters.
This helped in getting it to work in my particular case. – rgoraya Jul 30 '11 at 4:48.
Here is the working skeleton code: I created a new project and tried the combination of the other answers, and finally made it to work. Here is my solution, after that are the things to watch out for. I am using different models so bear with me: My models are: discussion has_many posts.
Discussion has no attributes. Posts has content:text and discussion_id:integer. Working Code (model) discussion.
Rb has_many :posts accepts_nested_attributes_for :posts (model) post. Rb belongs_to :discussion routes. Rb resources :discussions do resources :posts end (discussion view) _form.html.
Erb (controller) discussions_controller. Rb def new @discussion = Discussion. New @post = @discussion.posts.
Build respond_to do |format| format. Html # new.html. Erb format.
Xml { render :xml => @discussion } end end def create @discussion = Discussion. New(params:discussion) respond_to do |format| if @discussion. Save format.
Html { redirect_to(@discussion, :notice => 'Discussion was successfully created. ') } format. Xml { render :xml => @discussion, :status => :created, :location => @discussion } else format.
Html { render :action => "new" } format. Xml { render :xml => @discussion. Errors, :status => :unprocessable_entity } end end end Possible things that can go wrong First, Thilo was right, I get unknown attribute: post if I do # WRONG!
F. Fields_for :post Second, I have to have the @post instance variable in new action otherwise the post. Context textarea will not show up.
# REQUIRED! @post = @discussion.posts. Build Third, If I use the f.
Fields_for @post, the create action will complain unknown attribute: post too. # WRONG! F.
Fields_for @post do |p| Use this instead: # RIGHT! F. Fields_for :posts, @post do |p| The End So yeah, I wish we get to see more documentations on this (can't see any useful ones).
For example I see some use of form_for @discussion, @post but I can never get it to work.
Undeleted pending an edit by the poster into something more of an answer instead of a discussion about things that was tried. – Lasse V. Karlsen?
Jul 29 '11 at 13:54.
By using accepts_nested_attributes, you have created a setter method relationship_attributes=. There are a couple of things I noticed that need to change. You don't need to set @relationship = @issue.relationships.
Build Your form should be the following (you have f. Fields_for :relationship) = form_for @issue do |f| # your issue fields here = f. Fields_for :relationships do |r| # your relationship fields here The beauty here is that you won't have to set any ids or anything.
No, it should be f. Fields_for, otherwise they will just be fields out in the open. – Ryan Bigg Jul 27 '11 at 4:08 @Ryan: So if I do not set @relationship = @issue.relationship.
Build in the controller, I get the "undefined method `relationship' for #" Error. Kindly guide me. – rgoraya Jul 27 '11 at 4:23 1 @alock27: I am still getting the same error.
– rgoraya Jul 27 '11 at 4:26 @ryan that's correct, my mistake – alock27 Jul 27 '11 at 4:41.
I assume you are constructing the relationship in your controller and then trying to use it in the view. In order for this to be visible, you must make it an instance variable. All you need to do is throw an @ symbol in from of the name of relationship, as you have done with @issue.
@relationship = @issue.relationships. Build Edit: due to further information provided by the OP after the original question was asked, this answer is now clearly not applicable.
1 That should make no difference if the relationship variable is scoped only to the relevant action. – Thilo Jul 27 '11 at 1:05 @Jeremy: Thanks for the answer but even after doing this I am getting the same Error. Could you please suggest why this might be happening?
– rgoraya Jul 27 '11 at 1:15 I don't think this is the real fix for it. @relationship is not referenced in the view anywhere. – Ryan Bigg Jul 27 '11 at 4:07 Thilo: The view logic was originally not provided; I'd assumed that the name "relationship" was being used in the view as well, in which case it would not be found.
Ryan: you're correct. My previous diagnosis was based on the original information – Jeremy Roman Jul 27 '11 at 18:55.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.