Paul Barry
2006-Jan-21 21:05 UTC
[Rails] How do you deal with non-model property form values
I have a User ActiveRecord model that has email and password properties. I
want to build a login for that has a "remember me" option. My view
looks
like this:
<% @page_title = "Login" -%>
<%= error_messages_for ''user'' %>
<%= form_tag %>
<table>
<tr>
<td align="right" width="1%"
nowrap="nowrap"><label class="required"
for="user_email">Email</label>:</td>
<td><%= text_field("user", "email")
%></td>
</tr>
<tr>
<td align="right" width="1%"
nowrap="nowrap"><label class="required"
for="user_password">Password</label>:</td>
<td><%= password_field("user", "password")
%></td>
</tr>
<tr>
<td align="right"><input type="checkbox"
id="remember"
name="remember"/></td>
<td><label for="remember">Automatically log me in on
this
computer</label></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Login"
/></td>
</tr>
</table>
<%= end_form_tag %>
I would like to use the check_box FormHelper method, but remember is not a
property of the User object, and I would rather not add it, since it
doesn''t
real make sense. What''s the best way to handle this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060121/928fab6e/attachment-0001.html
Ben Reubenstien
2006-Jan-21 21:10 UTC
[Rails] How do you deal with non-model property form values
Hi Paul ~ You can pass it back in a separate hash. So you could set name extradata[remember], then access it separately from your user items in the form. params[:extradata][:remember] Hope this helps, ~ Ben On 1/21/06, Paul Barry <mail@paulbarry.com> wrote:> > I have a User ActiveRecord model that has email and password properties. > I want to build a login for that has a "remember me" option. My view looks > like this: > > <% @page_title = "Login" -%> > <%= error_messages_for ''user'' %> > <%= form_tag %> > <table> > <tr> > <td align="right" width="1%" nowrap="nowrap"><label class="required" > for="user_email">Email</label>:</td> > <td><%= text_field("user", "email") %></td> > </tr> > <tr> > <td align="right" width="1%" nowrap="nowrap"><label class="required" > for="user_password">Password</label>:</td> > <td><%= password_field("user", "password") %></td> > </tr> > <tr> > <td align="right"><input type="checkbox" id="remember" > name="remember"/></td> > <td><label for="remember">Automatically log me in on this > computer</label></td> > </tr> > <tr> > <td> </td> > <td><input type="submit" value="Login" /></td> > </tr> > </table> > <%= end_form_tag %> > > I would like to use the check_box FormHelper method, but remember is not a > property of the User object, and I would rather not add it, since it doesn''t > real make sense. What''s the best way to handle this? > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Ben Reubenstein http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060121/d334765a/attachment.html
Bill Katz
2006-Jan-22 05:23 UTC
[Rails] How do you deal with non-model property form values
Use check_box_tag() and an instance variable that isn''t in your User object. Like Ben says, it will still be accessible from params. In general, all the form helpers with _tag endings are useful for instance variables that are bookkeeping and not part of your real model. -Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060122/b97cf7e4/attachment.html
Paul Barry
2006-Jan-23 16:54 UTC
[Rails] How do you deal with non-model property form values
Ok, so I did this in my index.rhtml:
<%= check_box("form","remember") %>
And then my controller method looks like this:
def index
if request.get?
@user = User.new
else
@user = User.new(params[:user])
logger.info(params[:form][:remember])
flash[:notice] = "Invalid username/password"
end
end
when i submit the form, the form.remember value of 1 prints out, if I check
the remeber checkbox. But when the form renders again, the checkbox is
unchecked. Shouldn''t check_box handle setting the value of the
checkbox
correctly? I also tried check_box_tag, but neither one sets the value of
the checkbox correctly.
On 1/22/06, Bill Katz <billkatz@gmail.com> wrote:>
> Use check_box_tag() and an instance variable that isn''t in your
User
> object. Like Ben says, it will still be accessible from params. In general,
> all the form helpers with _tag endings are useful for instance variables
> that are bookkeeping and not part of your real model.
> -Bill
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060123/37308dd0/attachment.html
Alex Young
2006-Jan-23 16:59 UTC
[Rails] How do you deal with non-model property form values
Paul Barry wrote:> Ok, so I did this in my index.rhtml: > > <%= check_box("form","remember") %>That looks for an instance variable called @form with a method called ''remember''. What you probably want, judging by your controller code, is: <%= check_box_tag(''form[remember]'', params[:form][:remember]) %> And then remember to default it on the first time in your action. -- Alex
Paul Barry
2006-Jan-23 17:08 UTC
[Rails] How do you deal with non-model property form values
Yeah, I just set it to do this:
<%= check_box_tag("remember",1,params[:remember]) %>
I was using :form because that was Ben''s suggestion (he actually
suggested
using :extradata). That seems unnecessary. So I removed that hand modified
the controller to just call params[:remember].
Thanks for your help everyone
On 1/23/06, Alex Young <alex@blackkettle.org>
wrote:>
> Paul Barry wrote:
> > Ok, so I did this in my index.rhtml:
> >
> > <%= check_box("form","remember") %>
> That looks for an instance variable called @form with a method called
> ''remember''. What you probably want, judging by your
controller code, is:
>
> <%= check_box_tag(''form[remember]'',
params[:form][:remember]) %>
>
> And then remember to default it on the first time in your action.
>
> --
> Alex
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060123/7a913a27/attachment.html