我正在尝试从数据库中渲染一个随机对象。步骤:1. generate button
(主页)-> 2.调用rand_task
(控制器)-> 3. _rand_show.html.erb
(表单)
问题:它现在崩溃了,但有一次它起作用了。(我知道在编码中没有这样的事情,但我无法看到问题......)主要:
<%= form_for(:task, :url => {:action =>'rand_task', :id => 'random'}) do |f|%>
<%= render(:partial => "rand_show", :locals => {:f => f}) %></p>
<%end%>
控制器:
def rand_task
@task = Task.order('RANDOM()').first
end
形式:
<%= form_for(@task) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, :disabled=>true %>
<div class="field">
<%= f.label :category %><br />
<%= f.text_field :category, :disabled=>true %>
</div>
<div class="actions">
<%= f.submit "generate"%>
</div>
<% end %>
服务器:
Started GET "/" for 127.0.0.1 at 2012-09-13 19:35:56 +0300
Processing by TasksController#main as HTML
Rendered tasks/_rand_show.html.erb (0.8ms)
Rendered tasks/main.html.erb within layouts/application (1.6ms)
Completed 500 Internal Server Error in 4ms
ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
1: <%= form_for(@task) do |f| %>
2: <div class="field">
3: <%= f.label :name %><br />
4: <%= f.text_field :name, :disabled=>true %>
app/views/tasks/_rand_show.html.erb:1:in `_app_views_tasks__rand_show_html_erb__183960888__627313568'
app/views/tasks/main.html.erb:2:in `block in _app_views_tasks_main_html_erb__298912262__627495408'
app/views/tasks/main.html.erb:1:in `_app_views_tasks_main_html_erb__298912262__627495408'
如果我放置一个<%= form_for(@task _=Task.new_ ) do |f| %>
而不是删除它,它会起作用,但仅适用于一次