views:

1347

answers:

3

Hi, i'm having a problem to create a text_field without a method association. Maybe i even don't need it :-)

I have two radio_buttons associated to the same method:

<%= radio_button :comment, :author, "anonymous" %> Anonymous <br>
<%= radio_button :comment, :author, "real_name" %> Name <br>

What i would like to do is to have an text_field which when the user click on the radio_button "real_name" i can verify the value in this new text_field.

Basically my Controller would be something like:

@comment = Comment.new(params[:comment])

if @comment.author == "real_name" @comment.author = "value-from-the-new-textfield end

There is any way to do it?

Regards,

Victor

+4  A: 

If you want to generate a text_field without an associated object/method, use text_field_tag

Avdi
+1  A: 

You can use another parameter instead of :comment

<%= radio_button :verify, :author, "anonymous" %> Anonymous <br>
<%= radio_button :verify, :author, "real_name" %> Name <br>

So in your controller you can get the value of selected button with

if params[:verify][:author] == 'real_name' ...
JasonOng
A: 

text_field_tag is definitely the easiest way, but if you want to add a field that acts as part of a model, adding an attr_accessor attribute might be worth looking into as well.

Misplaced