Forum: Ruby on Rails simple question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
61e6493a2b24f914bfb4781319708bd3?d=identicon&s=25 Ben Newman (cbnewman)
on 2009-04-23 19:03
(Received via mailing list)
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 %><br />
    <% 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
8fa0f5c2bf8ad238baa4e20cb0360ebd?d=identicon&s=25 Matt Harrison (iwasinnamuknow)
on 2009-04-23 22:41
(Received via mailing list)
big poppa wrote:
> <%= f.label :make_this_true %><br />
> 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
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-04-23 22:43
I'd go for a hidden field too.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-04-23 23:47
(Received via mailing list)
On Apr 23, 2009, at 1:02 PM, big poppa wrote:
> <%= f.label :make_this_true %><br />
> 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.