提交的表单值不会显示在相应的视图中

时间:2012-02-12 18:15:35

标签: html ruby-on-rails ruby ruby-on-rails-3 post

以下是表格:

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

我的控制器:

class HomeController < ApplicationController
  def index
  end

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

在我的路由配置文件中:

post "home/tellafriend"

最后,我的观点:

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

为什么我在视图中显示的“名称”中输入的值不是?

我在POST时在控制台中看到了这个:

  

在2012-02-12 14:16:10开始发布“/ home / tellafriend”的127.0.0.1   -0400由HomeController #tellafriend处理为HTML

     

参数:{“utf8”=&gt;“✓”,
  “authenticity_token”=&gt;“2N1jNQ30cXCU4YANQ3FEZFBBTNhKobCQUwj1rEZ3Mxw =”}

     

在布局/应用程序中呈现home / tellafriend.html.erb(0.0ms)   在20ms完成200 OK(浏览次数:11.9ms | ActiveRecord:0.0ms)

这是否意味着我的价值观没有公布?建议?


修改

在每个HTML输入元素的name属性中添加,值现在正在POSTED:

  

在2012-02-12 14:23:39为127.0.0.1开始发布“/ home / tellafriend”   -0400由HomeController处理#tellafriend作为HTML参数:{“utf8”=&gt;“✓”,   “authenticity_token”=&gt; “中2N1jNQ30cXCU4YANQ3FEZFBBTNhKobCQUwj1rEZ3Mxw =”,   “name”=&gt;“Sergio”,“email”=&gt;“stapia.gutierrez@gmai”,   “message”=&gt;“asdf”}在home / tellafriend.html.erb中呈现   布局/应用(0.4ms)在40ms完成200 OK(浏览次数:26.9ms |   ActiveRecord:0.0ms)

但是View仍然不会渲染值。

1 个答案:

答案 0 :(得分:1)

您的input字段缺少name属性:

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

更新答案:

erb中的

<% %>执行括号内的代码,但不会打印到模板。

您还需要更改视图:

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

相关问题