Topic: newbie question about form

hello all,

i used a scaffold to generate a form.
what i need to know is if i add a text entry input, how to i seperate it out of the params ?

for example, this part is generated by the form

<%= text_area("outbox", "message") %>

and i want to add this

<label for="other_email">email</label>
<input type="text" name="email" >

now when the form is submitted to the contact action, i have this
def contact
      @outbox = Outbox.create(params[:outbox])
      if ! params[:email].nil?
          @outbox.message += "\ncontact email = #{params[:email]}"
      end
      @outbox.save
      redirect_to :action => "confirm_contact"
end

but it is not working right.
is there something i am missing ( or some things )

thanks for any tips

Re: newbie question about form

It looks like this should work, what specifically is not working?

There are a few better ways to handle this though. Probably the best way is to add a column to your Outbox model called "email". You can then create a text field like this:

<%= text_field :outbox, :email %>

Then you don't have to worry about it in the controller:

def contact
  Outbox.create(params[:outbox])
  redirect_to :action => "confirm_contact"
end

If you ever need the message with the email you can create a custom method for that in the Outbox model. For example:

# in Outbox model
def message_with_email
  "#{message}\ncontact email = #{email}"
end

Railscasts - Free Ruby on Rails Screencasts

Re: newbie question about form

well, what i am going for is taking the message, and if someone enters an email address, i could add it to the message at the bottom or something.

it just isn't working so far.

thanks for your help, and your help before on this site, ryanb

Re: newbie question about form

Can you go into detail on what isn't working?

Railscasts - Free Ruby on Rails Screencasts

Re: newbie question about form

well, its this part

@outbox.message += "\ncontact email = #{params[:email]}"

in the contact action..

it is creating a record, but not adding the text from the email parameter to it.

thanks !

Re: newbie question about form

Since you are saving the record again after setting this, I don't see why it isn't working. The only thing I can think of is the email parameter isn't being passed for some reason. Check your development.log file and see if this is the case.

Railscasts - Free Ruby on Rails Screencasts

Re: newbie question about form

ok. i may not be doing the form correctly.
here is what i have.. i did look at the development log, and i found this

Parameters: {"outbox"=>{"message"=>"some text for the email message", "header"=>"request pivotrac info", "address"=>"nephish@gmail.com"}, "commit"=>"send", "action"=>"contact", "controller"=>"outbox", "phone"=>"some phone", "email"=>"some email"

and here is the form (without some stuff like <br> here and there)
<%= form_tag(:action => "contact", :controller => 'outbox') %>
<label for="other_email">email</label>
<input type="text" name="email" >

<label for="other_phone">phone </label>
<input type="text" name="phone">

<%= text_area("outbox", "message") %>
<%= hidden_field("outbox", "address", :value => 'somedude_o1@gmail.com') %>
<%= hidden_field("outbox", "header", :value => 'request site info') %>
<%= submit_tag("send") %>

<%= end_form_tag %>


thanks for your help on this !

Re: newbie question about form

It looks correct to me. You can try appending it to the params before creating the outbox.

def contact
      params[:outbox][:message] += "\ncontact email = #{params[:email]}" unless params[:email].blank?
      Outbox.create!(params[:outbox])
      redirect_to :action => "confirm_contact"
end

I added an exclamation mark to the create action to report any validation errors - this might be the cause of it not saving before.

Railscasts - Free Ruby on Rails Screencasts

Re: newbie question about form

i tried it this way, but same thing is happening.
sorry, i don't quite get it.
it writes the header, the message text, but does not append anything to it.

weird

ok, again, thanks for your help with this.

sk

Re: newbie question about form

Hmm, weird, guess we'll keep trying different things:

def contact
      params[:outbox][:message] += "\ncontact email = #{params[:email]}"
      Outbox.create!(params[:outbox])
      redirect_to :action => "confirm_contact"
end

Railscasts - Free Ruby on Rails Screencasts

Re: newbie question about form

that worked !
My God, i have never found the kind of commitment to helping a newbie as i have found on this site, thanks a lot for not giving up. I would have by now, but my job kinda requires me to get this right. ( i am the one that talked the uppers into doing a rails app wink .
again, thanks

Re: newbie question about form

Glad to hear it worked. I'm still puzzled why removing the "unless" condition would solve the problem. So params[:email] isn't blank but the condition expression thought it was. Oh well.

Railscasts - Free Ruby on Rails Screencasts

Re: newbie question about form

likely  a typo on my part.

look forward to posting my next stumper.

thanks again