List Info

Thread: Re: Help with forms and joined tables?




Re: Help with forms and joined tables?
country flaguser name
Germany
2008-02-23 16:34:53
Hey guys, that got me on the right track!  I used the
form_tag, but had 
to remove the '=' and also add a 'do' for it to work.  I
also just used 
a HTML textarea element named 'comment'.  That got the view
corrected, 
but I then started receiving an error saying:

undefined method 'stringify keys!' for "text added for
comment 
example":String

To remedy that, I updated the controller so that the field
to which I 
was inserting (body) was explicitly named like this:

    Post.find(params[:id]).comments.create(:body =>
params[:comment])

Thanks Jeff and Ilan for the assist!

Derek
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Help with forms and joined tables?
country flaguser name
Germany
2008-02-23 19:23:23
Derek Knorr wrote:
>     Post.find(params[:id]).comments.create(:body =>
params[:comment])

This suggests your Comment model's attribute is
"body".  It seems 
unlikely that you have a special page just to create a
comment, so I'm 
going to assume that this form is on the Post show view. 
What you 
probably need then is something like this:

Post controller:
----------------------------------------
def show
  # Get the post and all current comments for display
  # including the comments here means 1 hit on the db
  post = Post.find(params[:id], :include =>
:comments)
  # Create a blank comment to feed to the form
  comment = Comment.new
  # any other initialisation stuff
end
----------------------------------------
The show view (just the bit for the new comment):
----------------------------------------
<% form_for :comment, :url => {:action =>
:add_comment, :id => post} do 
|f| %>
  Add a comment:<br />
  <%= f.text_area :body %>
  <br />
  <%= f.submit "Save" %>
<% end %>
----------------------------------------
back in the controller:
----------------------------------------
def add_comment
  Post.find(params[:id]).comments.create(params[:comment])
end
----------------------------------------

The reason for this is that the name of the form (here
:comment) means 
to take the instance variable of the same name (so comment)
set in the 
controller for the current default values.  The names of the
fields (now 
:body in my example) mean use that attribute of the form
object (so 
comment.body here) as the default value for that
field.  Since the 
comment is a blank one, we'll get a blank field.

When the form is submitted, it will send parameters back to
the 
controller with keys :id (from the url spec) and :comment
(from the name 
of the form).  The parameter with key :comment will have a
hash as a 
value.  That hash will have keys of field names and values
of the data 
from the field.  So, if you entered "comment text"
in the form and the 
post was id 1, the parameters sent to add_comment would be
the 
equivalent of:
{:id => 1, :comment => {:body => "comment
text"}}

This is why we can use params[:comment] now as an argument
to the create 
(it is exactly the hash you need).  The reason for your no
comment= 
method should now be apparent. Because you used :comment as
the name of 
your text field, the parameters list sent back to the
controller was the 
equivalent of:
{:id => 1, :comment => {:comment => "comment
text"}}

The second :comment there was being interpreted as the field
to set, so 
it was trying to call the comment= method to do this.

Whenever I am playing with forms in Rails, I always compare
the view 
with the generated HTML and the parameters sent back to the
controller 
(from the log file) so that I am clear where values are
coming from and 
going to.  It can save a lot of headaches.
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )