Only Bolivian Here
Only Bolivian Here

Reputation: 36733

Submitted form values aren't shown in the corresponding View

Here is the form:

      <%= form_tag({:controller => "home", :action => "tellafriend"}, :method => "post", :class => "well form-horizontal") do %>
          <div class="control-group">
            <label class="control-label" for="input01">Your name:</label>
            <div class="controls">
              <input id="name" class="input-xlarge" type="text"/>
              <p class="help-block">Type in your name so your friends know you sent this.</p>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label" for="input01">Friends email address:</label>
            <div class="controls">
              <input id="emails" class="input-xlarge" type="text"/>
              <p class="help-block">Who do you want to send this to? Separate different emails with a comma.</p>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label" for="input01">Message:</label>
            <div class="controls">
              <textarea id="message" class="input-xlarge" type="text" rows="7"></textarea>
              <p class="help-block">Attach a special message your friends will read.</p>
            </div>
          </div>
          <button class="btn tell-a-friend-submit" type="submit">Send</button>
        <% end %> 

And my Controller:

class HomeController < ApplicationController
  def index
  end

  def tellafriend
    @name = params[:name]
    @emails = params[:emails]
    @message = params[:message]
  end  
end

And in my Routes configuration file:

post "home/tellafriend"

And finally, my View:

<p><% @name %></p>

Why isn't the value I enter in "name" shown in the View?

And I see this in my console when I do the POST:

Started POST "/home/tellafriend" for 127.0.0.1 at 2012-02-12 14:16:10 -0400 Processing by HomeController#tellafriend as HTML

Parameters: {"utf8"=>"✓",
"authenticity_token"=>"2N1jNQ30cXCU4YANQ3FEZFBBTNhKobCQUwj1rEZ3Mxw="}

Rendered home/tellafriend.html.erb within layouts/application (0.0ms) Completed 200 OK in 20ms (Views: 11.9ms | ActiveRecord: 0.0ms)

Does this mean my values aren't being posted? Suggestions?


Edit:

Added in the name attribute for each HTML input element and the values are now being POSTED:

Started POST "/home/tellafriend" for 127.0.0.1 at 2012-02-12 14:23:39 -0400 Processing by HomeController#tellafriend as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"2N1jNQ30cXCU4YANQ3FEZFBBTNhKobCQUwj1rEZ3Mxw=", "name"=>"Sergio", "emails"=>"stapia.gutierrez@gmai", "message"=>"asdf"} Rendered home/tellafriend.html.erb within layouts/application (0.4ms) Completed 200 OK in 40ms (Views: 26.9ms | ActiveRecord: 0.0ms)

However the View still doesn't render the values.

Upvotes: 0

Views: 87

Answers (1)

osahyoun
osahyoun

Reputation: 5231

Your input fields are missing a name attribute:

<input name="name" id="name" class="input-xlarge" type="text"/>

UPDATE TO ANSWER:

<% %> in erb executes the code within the brackets, but it does not print to template.

You'll also need to change in your view:

<p><% @name %></p> to <p><%= @name %></p>

Upvotes: 1

Related Questions