Seamus Gilchrist
2006-Jul-27 16:31 UTC
[Rails] setting initial text field values in a form
Hi, I am trying to create a basic form in which I want one of the fields initialized before displaying it. My code (in a view) is as follows: <% if ( session[:user_id] != nil ) then logged_in_user = User.find(session[:user_id]) end %> <% form_for :suggestion do |form| %> <label for="suggestion_title">Topic:</label><br/> <%= form.text_field :title, :size => 64 %> <label for="suggestion_email">Email:</label><br/> <%= form.text_field :email, :size => 64 %> <label for="suggestion_suggestion">Suggestion:</label><br/> <%= form.text_area :suggestion, :size => 1000, :cols => 48 %> <%= submit_tag "Send Feedback", :name=>"addsuggestion" %> <% end %> I would like to set the email address in the form field such that it displays the email address from the logged_in_user (i.e., logged_in_user.email) any ideas on how to do this would be appreciated. I have tried multiple times using things such as :suggestion.email = logged_in_user.email :email = logged_in_user.email appreciate the input -- Posted via http://www.ruby-forum.com/.
On Thursday, July 27, 2006, at 6:30 PM, Seamus Gilchrist wrote:>Hi, > >I am trying to create a basic form in which I want one of the fields >initialized before displaying it. > >My code (in a view) is as follows: > ><% > if ( session[:user_id] != nil ) > then > logged_in_user = User.find(session[:user_id]) > end >%> > ><% form_for :suggestion do |form| %> > ><label for="suggestion_title">Topic:</label><br/> ><%= form.text_field :title, :size => 64 %> ><label for="suggestion_email">Email:</label><br/> ><%= form.text_field :email, :size => 64 %> ><label for="suggestion_suggestion">Suggestion:</label><br/> ><%= form.text_area :suggestion, :size => 1000, :cols => 48 %> ><%= submit_tag "Send Feedback", :name=>"addsuggestion" %> ><% end %> > >I would like to set the email address in the form field such that it >displays the email address from the logged_in_user (i.e., >logged_in_user.email) any ideas on how to do this would be appreciated. > >I have tried multiple times using things such as >:suggestion.email = logged_in_user.email >:email = logged_in_user.email > >appreciate the input > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails+1 slap on the wrist for putting business logic in a view.... However, a better way to do this is -- controller def action @suggestion = Suggestion.new @suggestion.email = User.find(session[:user_id]).email end -- view <%= text_field ''suggestion'', ''email'' %> If you initialize the object with default values in the action, you will see them in the view when it renders. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Seamus Gilchrist
2006-Jul-27 18:11 UTC
[Rails] Re: setting initial text field values in a form
Kevin Olbrich wrote:> On Thursday, July 27, 2006, at 6:30 PM, Seamus Gilchrist wrote: >> logged_in_user = User.find(session[:user_id]) >><%= form.text_area :suggestion, :size => 1000, :cols => 48 %> >> >>appreciate the input >> >>-- >>Posted via http://www.ruby-forum.com/. >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > +1 slap on the wrist for putting business logic in a view.... >Point taken - got it working as you suggested - thanks very much -- Posted via http://www.ruby-forum.com/.