form_for无法呈现以查看但正确生成html

时间:2012-03-25 17:55:57

标签: ruby-on-rails-3 form-for

我的观点中有以下表格:

<%= form_for @new_home, :url => {:controller => "homes", :action => "create"} do |f| %>
       <%= f.hidden_field :user_id, :value => current_user.id %>
       <%= f.label :name, "Home Name:" %><br />
       <%= f.text_field(:name) %>
       <%= f.submit("Add New Home", :class => "green_button") %>
<% end %>

和控制器:

def main
    @new_home = Home.new
end

生成的html是:

<form accept-charset="UTF-8" action="/homes/create" class="new_home" id="new_home" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="lyvJq8FmFoOvzi7LYNhkrY8t9WI9phtBlNdGvMOFoF8=" />
    <input id="home_user_id" name="home[user_id]" type="hidden" value="1" />
    <label for="home_name">Home Name:</label><br />
    <input id="home_name" name="home[name]" size="30" type="text" />
    <input class="green_button" name="commit" type="submit" value="Add New Home" />
</form>

出于某种原因,我看不到我的视图中显示的表单。没有生成错误。我只看到一张没有表格的空白页面。我没有应用任何jquery或css来隐藏表单。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为你错过了div字段和动作类。见下文。

<%= form_for @new_home, :url => {:controller => "homes", :action => "create"} do |f| %>
   <%= f.hidden_field :user_id, :value => current_user.id %>
  <div class="field">
   <%= f.label :name, "Home Name:" %><br />
   <%= f.text_field :name %>
  </div>
  <div class="action">
   <%= f.submit("Add New Home", :class => "green_button") %>
  </div>
<% end %>

如果它不起作用,请告诉我!