Rails 4 - 在表格中显示依赖模型 [英] Rails 4 - Displaying a dependent model in a table

查看:31
本文介绍了Rails 4 - 在表格中显示依赖模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文章模型,有一个依赖模型 Photocup.每个 Photocup 实例都有一个附加的图像(使用回形针/imagemagick).我按照 rails 指南添加了第二个模型,就像评论与文章相关联一样.

I have an Article model, with a dependent model Photocup. Each Photocup instance has an image attached (using paperclip/imagemagick). I followed the rails guide for adding a second model, the same way Comments are associated with an Article.

http://guides.rubyonrails.org/getting_started.html#添加第二个模型

它工作正常.但我想要更多地控制 Photocups 的显示方式.我在视图/文章中创建了一个部分 _gallery.html.erb.画廊部分看起来像这样:

It works fine. But I want more control over how the Photocups are displayed. I've created a partial _gallery.html.erb in Views/Article. The gallery partial looks like this:

  Photo Gallery
 <table class="table2">
 <%= render @article.photocups %>
 </table>

Views/Photocup 中的_photocup.html.erb 看起来像这样"

_photocup.html.erb in Views/Photocup looks like this"

 <td>
 <%= photocup.label %>
 <br> 
 <%= image_tag photocup.image(:small) %>
 <br>
  <%= link_to 'Remove Photo', [photocup.article, photocup],
           method: :delete,
           data: { confirm: 'Are you sure?' } %>
 </td>

工作正常,但我不知道如何连续添加!

Works fine, but I can't figure out how to add in a row!

无论如何,是否可以像使用非依赖模型一样显示我正在查看的文章的 Photocups?例如,一个索引页面通常会有这样的内容"

Is there anyway to display the Photocups for the Article I'm looking at the way one does with non-dependent model? For example, an index page would typically have something like this"

 <table>
 <tr>
 <th>Title</th>
 </tr>
 <%= @photocups.each do |photocup| %>
 </tr>
 <td><%= photocup.title %></td>
 <% end %> 
 </table>

这能以某种方式在文章的 show.html.erb 页面上完成吗?如果我在 Views/Photocup 中创建了一个部分,可以完成吗?

Could this be done on the show.html.erb page for the article somehow? Could it be done if I created a partial in Views/Photocup?

如果重要,路线如下所示:

If it matters, the route looks like this:

 resources :articles do
 resources :comments
 resources :photocups
 get 'gallery' 
 get 'showall'
 get 'feature', on: :collection
 end 

在此先感谢您的帮助.

推荐答案

可以在articles#show页面.如果您的关联正确完成,您将获得:

It can be on the articles#show page. Provided your associations are done correctly, you have a:

#article.rb
Class Article > ActiveRecord::Base
has_many :photocups
end 

#photocup.rb
Class Photocup > ActiveRecord::Base
belongs_to :article 
end

您必须在文章控制器中的 show 操作下声明一个实例变量.它看起来像这样:

You'll have to declare an instance variable under the show action in the Articles controller. It'll look something like this:

#articles_controller.rb
def show
         @article=Article.find(params[:id])
         @photocups=@article.photocups
    end

然后在显示视图中使用示例索引视图的代码,它应该显示您正在寻找的行.

Then use the code for your example index view in your show view and it should show the rows you are seeking.

另外,旁注,这一行:

<%= @photocups.each do |photocup| %>

在您的示例索引页中不需要 =.这些应该保留给您希望用户在视图中看到的内容,应该是:

in your example index page does not need an =. Those should be reserved for things you want the user to see in the view, it should be:

<% @photocups.each do |photocup| %>.

这篇关于Rails 4 - 在表格中显示依赖模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆