tomh
2007-Sep-22 18:16 UTC
Rails newbie: passing a value to hidden form field, then getting it
Warning: I am as green as #00FF00 with rails :-)
I have an rhtml view that shows a survey: /views/survey/show.rhtml. I
have a Survey model, helper and controller. I''m using the
acts_as_commentable plugin to collect comments, and trying to save
user comments from users who have taken a survey (don''t ask --
it''s
just a test).
To save the comment, I need the id of the survey, which I have when I
show the page originally (but which is gone when the user hits the
submit button to save their comment. So I am just trying to save the
survey id in a hidden field associated with the form.
In the show.rhtml, I have:
<% form_for :comment, @survey, :url => { :action => :save_comment }
do |form| %>
<%= form.hidden_field "survey", "#{@survey.id}"
%>
...
<% end %>
Which results in "ActionView::TemplateError (undefined method
`merge''
for "1":String)". 1 is the id of the survey, so I am close. I
tried
numerous variations of passing values via form_for to a hidden field.
Question 1: How to pass the value so I can get at it when the user
hits submit
In the survey_controller.rb I have
def save_comment
@comment = Comment.new(params[:comment])
logger << "COMMENT #{@comment.comment}\n"
# @survey = Survey.find(?????????) # somehow I need to get that
survey id so I can do the next line
@survey.add_comment(@comment)
end
Question 2: what magic do I need to get at the value in the hidden
field?
Like I said, I am a noob.
Thanks for your tolerance of my ignorance.
Tom
--~--~---------~--~----~------------~-------~--~----~
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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hassan Schroeder
2007-Sep-22 19:05 UTC
Re: Rails newbie: passing a value to hidden form field, then getting it
On 9/22/07, tomh <tom.harrrison.jr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Warning: I am as green as #00FF00 with rails :-)heh, me too :-) but...> submit button to save their comment. So I am just trying to save the > survey id in a hidden field associated with the form. > > In the show.rhtml, I have: > > <% form_for :comment, @survey, :url => { :action => :save_comment } > do |form| %> > <%= form.hidden_field "survey", "#{@survey.id}" %>Per the doc: <http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000496> :: a hidden_field takes the same args as a text_field, namely: text_field(object_name, method, options = {}) So I think you need something like: hidden_field( "comment", "survey", { :value => @survey.id } ) No warranty, express or implied, as they say :-) HTH! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---