执行摘要
亲爱的朋友们,
正如你所看到的,我有post.title,post.subject;我遍历每个@ posts.each,然后将每个单独的对象传递给一个表单!是什么让它声称我没有物品。这太奇怪了?
型号:
class Post < ActiveRecord::Base
attr_accessor :title, :subject
end
路线:
resources :admin_dashboards
admin_dashboards_controller.rb
class AdminDashboardsController < ApplicationController
def index
@posts = Post.all()
end
查看:index.html.erb
<tr>
<% @posts.each do |post| %>
<%= form_for post do |single_post| %>
<td> <%= single_post.subject %> </td>
<td> <%= single_post.submit %> </td>
</tr>
<% end %>
<% end %>
scheme.rb
create_table "posts", force: true do |t|
t.text "title"
t.text "subject"
t.text "url"
t.datetime "created_at"
t.datetime "updated_at"
end
答案 0 :(得分:4)
form_for块的参数不是post,它是FormBuilder。尝试这样的事情:
<tr>
<% @posts.each do |post| %>
<%= form_for post do |single_post| %>
<td> <%= single_post.text_field :subject %> </td>
<td> <%= single_post.submit %> </td>
<% end %>
<% end %>
</tr>