Simple question


#1

I apologize for the embarrassingly simplistic nature of this question,
but I’m new to Ruby/Rails.

From within a html.erb file, what is the syntax for assigning a value
to a parameter? For example, when a specific form is used, the
following attribute (make_this_true) should always be 1

<% form_for(@foo) do |f| %>
<%= f.error_messages %>

<%= f.label :make_this_true %>

<% foo.make_this_true = 1 %>

but I don’t have to tell the readers of this group that the above code
is garbage.

  1. is there an easy way to set this attribute? If so, what is the
    syntax?
  2. My first attempt at solving this problem involved using a check box
    or a radio button that defaulted to “true” but this turned out to be
    non-intuitive as well. I then realized that the user should not need
    to interact with this variable here.
  3. Perhaps making the default value of this variable true in the
    database schema? Any tips on that route?

Sorry for the long-windedness and simplistic nature of this question.

–b


#2

big poppa wrote:

<%= f.label :make_this_true %>

to interact with this variable here.
3. Perhaps making the default value of this variable true in the
database schema? Any tips on that route?

I’d do it with a hidden field, others may be able to recommend something
else though.

HTH

~Matt


#3

I’d go for a hidden field too.


#4

On Apr 23, 2009, at 1:02 PM, big poppa wrote:

<%= f.label :make_this_true %>

to interact with this variable here.
3. Perhaps making the default value of this variable true in the
database schema? Any tips on that route?

Sorry for the long-windedness and simplistic nature of this question.

–b

In your controller:

new:
@foo = Foo.new(:make_this_true => true)
new.html.erb
<%= f.hidden :make_this_true %>

or, create:
@foo = Foo.new(params[:foo]) {|f| f.make_this_true = true }

Having a default in the DB schema is likely a good idea,too. If this
is actually a boolean, having :default=>true,:null=>false keeps you
from dealing with NULL values in the database.

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid