【问题标题】:Dynamically generate link_to for different models using common partial - Rails 4使用通用部分动态生成不同模型的link_to - Rails 4
【发布时间】:2016-12-30 06:33:47
【问题描述】:

我想使用一个通用的部分来呈现一个列表页面,并且它的运行良好期望我面临一个问题,为:edit 和 :delete 操作生成动态链接,这是常见的。

所以我在两个不同的视图中有@menu 和@picture 模型,呈现相同的公共部分(我创建的)。

这是我常见的部分。

shared/_index_common_grid.html.erb

               <% pictures.each do |picture| %>
                  <div class="col-sm-4 pull-left">
                      <div class="thumbnail">
                          <div class="caption"> 
                               <h4><%= picture.title.capitalize%></h4>
                              <p class="text-muted">
                              <!--THIS IS WHERE I WANT TO HANDLE DYNAMIC GENERATION OF LINK_TO FOR @picture and @menu models --> 
                                 <%= link_to "Edit", [:edit, current_user,@request,@shop,picture]%>
                              |  <%= link_to "Delete", [current_user,@request,@shop,picture],:data=>{:confirm=>"Are you sure ?"}%>
                              | <span class="pull-right"><%= show_formatted_date(picture.created_at)%></span>                                
                              </p>
                          </div>
                      </div>
                  </div> 
                <%end%>

这是我使用上述常见部分的一个视图,除了我传递不同的模型之外,还有一个与此相似的视图。

##my view page --------pictures/index
                  <%unless @pictures.blank?%>   
                      <%= render partial: "shared/index_common_grid", locals: {pictures: @pictures}%>
                  <%end%>

我不想在 application_helper 中使用 switch case,这很容易完成。

【问题讨论】:

  • 所以你使用这个部分来列出picturesmenus?只是问题是图片和菜单的编辑和删除链接会有所不同吗?菜单所需的params 是什么?你是在rails 3还是rails 4上?
  • 它的 Rails 4(在问题中提到:0)。是的..我想要每个资源的动态链接...
  • 那么menus 的链接是什么?意味着rails中有url_for之类的选项可能会对您有所帮助。但这取决于每个参数所需的参数。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

您可以将布局与您的部分一起使用。有关详细信息,请参阅 API 文档。

使用布局渲染图片

首先,将pictures/index.html.erb改写为:

<%= render partial: 'picture', layout: 'shared/index_common_grid', collection: @pictures, as: :object %>

其次,将shared/index_common_grid.html.erb改写为:

<div class="col-sm-4 pull-left">
  <div class="thumbnail">
    <div class="caption"> 
      <h4><%= object.title.capitalize%></h4>
      <p class="text-muted">
      <%= yield %> | <span class="pull-right"><%= show_formatted_date(object.created_at)%></span>                                
      </p>
    </div>
  </div>
</div> 

第三,创建pictures/_picture.html.erb

<%= link_to "Edit", [:edit, current_user, @request, @shop, picture]%>
|  <%= link_to "Delete", [current_user, @request, @shop, picture], :data=>{:confirm=>"Are you sure ?"}%>

添加其他类型的对象

如果您想渲染其他类型的对象(示例中的菜单),那么:

  1. 添加menus/index.html.erb,其内容类似于pictures/index.html.erb(将picture@pictures 替换为menumenus)。
  2. 添加menus/_menu.html.erb 并为菜单适当呈现链接。

【讨论】:

  • 感谢@Greg...这是一个很好的方法,但我正在寻找一种仅通过一种 CONCERN 方法来处理所有对象的方法...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2015-07-19
  • 2015-07-09
  • 2012-12-13
相关资源
最近更新 更多