Rails newbie: passing a value to hidden form field, then get

Warning: I am as green as #00FF00 with rails :slight_smile:

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

On 9/22/07, tomh remov[email protected] wrote:

Warning: I am as green as #00FF00 with rails :slight_smile:

heh, me too :slight_smile: 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 :slight_smile:

HTH!

Hassan S. ------------------------ [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs