【发布时间】:2014-10-16 16:02:42
【问题描述】:
我正在尝试在我的控制器中使用包含(刚刚学习了这个概念),但是由于某种原因,它没有被我的索引视图内的每个循环中的导轨所接受。
<% @articles.each do |latest|%>
<div class="row homepage_article text-left">
<div class="col-md-3">
<% if latest.photos.present? %>
<%= image_tag latest.photos.first.image.url(:medium),:class => 'img- responsive' %>
<% else %>
<img src="http://placehold.it/400x300" class="img-responsive">
<% end %>
</div>
<div class="col-md-9">
<h2 class="title"><%= latest.title %></h2>
<p class="byline"><%= latest.author %></p>
<p class="summary"><%= latest.content.split(/\s+/)[0..100].join(' ') %></p>
</div>
</div>
<hr>
<% end %>
控制器:
def index
@articles=Article.includes(:content).all.order(created_at: :desc).paginate(:page => params[:page], :per_page => 3)
@carousel_article=Article.all.order(created_at: :desc).limit(3)
#@carousel_article=Article.all.order(created_at: :desc).paginate(:page => params[:page], :per_page => 3)
@user=current_user
end
错误:
Association named 'content' was not found on Article; perhaps you misspelled it?
Extracted source (around line #39):
<% @articles.each do |latest|%>
<div class="row homepage_article text-left">
<div class="col-md-3">
<% if latest.photos.present? %>
在此处编辑以包括模型。如您所见,内容是一个领域。谢谢!
create_table "articles", force: true do |t|
t.string "title"
t.text "content"
t.string "category"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.text "bio"
t.string "password"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
【问题讨论】:
-
你能发布你的文章和内容模型吗?
-
create_table "articles", force: true do |t| t.string "title" t.text "content" t.string "category" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" end -
includes 是
association的一种方法,正如 diego 在他的回答中所解释的那样
标签: ruby-on-rails view controller include