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| %>

Topic:

<%= form.text_field :title, :size => 64 %>
Email:

<%= form.text_field :email, :size => 64 %>
Suggestion:

<%= 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

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
[email protected]
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

Kevin O. 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
[email protected]
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