Rails - 在视图中渲染视图 - 传递参数

时间:2016-10-17 15:42:35

标签: ruby-on-rails erb ruby-on-rails-5

在我的应用中,我可以使用以下方式调用视图(' edit.html.erb')

select * from table_name where column_name not like LTRIM(RTRIM(column_name))

如果我想在另一个视图中渲染它,那等价是什么?

我如何将它传递给params?

修改

实际上我有一个父母的视图,我想在其中列出孩子(可编辑)。这些我也可以单独编辑。简单的解决方案是,我创建一个_partial,我从父视图和<%= link_to( tag.content, [object, tag], method: :patch) %> 创建。

我可以使用以下链接调用此edit.html.erb(或更新):

edit.html.erb

这很有效。现在问题是如何使用渲染渲染部分(让我们称之为<%= link_to( tag.content, [object, tag], method: :patch) %> )?仍然需要传递父(即对象)和子(标签)参数。如何使用_update.html.erb

执行此操作

编辑完整示例

我想要实现的目标(简而言之) - 允许在注释视图(父级)中编辑标签(子)。

对于模型/对象&#34; Annotation&#34;的现有记录,我想为附加的PDF添加标签。我在名为&#34; Annotate&#34;的页面/视图中执行此操作,我从Annotation编辑视图打开(使用&#39; link_to&#39;)。这&#34; Annotate&#34;页面有2列(见下面的截图):

  1. 左侧窗格:2个部分 - 1个用于快速添加标记,2个用于编辑现有标记(在表格中列出)
  2. 右窗格:PDF
  3. enter image description here

    标签和注释具有多态关系(按照此question设置)

    注释的

    <% render partial "tags/update" PARAMS %>

    _form.html.erb

    注释视图 <div class="row"> <div class="col-md-6"> <%= simple_form_for @annotation, html: { class: 'form-horizontal', multipart: true }, wrapper: :horizontal_form, wrapper_mappings: { check_boxes: :horizontal_radio_and_checkboxes, radio_buttons: :horizontal_radio_and_checkboxes, file: :horizontal_file_input, boolean: :horizontal_boolean } do |f| %> <div class="btn-toolbar btn-group", role="toolbar"> <%= f.button :submit, 'Save', :class => "btn btn-xs btn-default" %> <%= link_to 'List' , annotations_path, :class => "btn btn-xs btn-default" %> <% unless @annotation.file.blank? %> <%= link_to 'Annotate', annotate_path(@annotation), :class => "btn btn-xs btn-default" %> <% end -%> </div> <h4>Annotation</h4> <%= f.error_notification %> <% if @annotation.file.blank? %> <%= f.input :file, as: :file, input_html: { accept: ('application/pdf') } %> <% else %> <% end -%> <%= f.input :name, placeholder: 'Enter name' %> <%= f.input :description, placeholder: 'Description', :input_html => { :rows => 3 } %> <%= f.association :documenttype, :collection => Documenttype.active.order(:name), prompt: 'Select document type' %> <%= f.association :sender, label: 'Submitted by' , prompt: 'Select sender' %> <%= f.association :receiver, label: 'Specific to', prompt: 'Select recipient' %> <%= f.input :active, as: :boolean %> <% end -%> </div> <% unless @annotation.file.blank? %> <div class="col-md-6"> <%= content_tag :iframe, nil, src: pdf_annotation_path(@annotation), width: "100%", height: "770px", frameBorder: "0" %> </div> <% end %> </div> <% unless @annotation.new_record? %> <div class="row"> <hr> <div class="col-md-6"> <%= render @annotation.comments %> </div> <div class="col-md-6"> <%= render 'comments/form', :object => @annotation %> </div> </div> <% end -%>

    annotate.html.erb

    标记列表 <div class="row"> <div class="col-lg-5"> <div class="btn-toolbar btn-group" role="toolbar"> <%= link_to 'Close', annotation_path(@annotation), :class => "btn btn-xs btn-default" %> <%= link_to 'List' , annotations_path, :class => "btn btn-xs btn-default" %> </div> <h4>Annotate document</h4> <div data-spy="affix"> <%= render 'tags/form', :object => @annotation %> <br> <div class="panel panel-default" id="annotationResults"> <%= render 'tags/tag_list', :object => @annotation %> </div> </div> </div> <div class="col-lg-7" id="file"> <%= content_tag :iframe, nil, src: pdf_annotation_path(@annotation), width: "100%", height: "875px", frameBorder: "0" %> </div> </div>

    _tag_list.html.erb

    标记更新表单 <table id="tags" class="table table-hover" style="background-color: white; word-wrap: break-word; font-size: 0.9em;" > <thead> <tr> <th>Tagged content</th> <th>as</th> <th>in</th> <th></th> </tr> </thead> <tbody> <% object.tags.each do |tag| %> <% unless tag.content.blank? %> <tr data-tag-id='<%= tag.id %>', class='show-tag'> <td style="word-wrap: break-word;"><%= link_to( tag.content, [object, tag], method: :patch) %> <td><%= tag.tagtype.name %></td> <td><%= tag.tagtype.typeoftag %></td> <td><%= link_to '', [object, tag], method: :delete, data: { confirm: 'Please confirm!' }, :class => "glyphicon glyphicon-trash" %></td> </tr> <tr data-tag-id='<%= tag.id %>', class='edit-tag'> <td colspan="4"><%= render partial: 'shared/tester' %><%#= render partial: 'tags/update', object: @tag.tagable, tag: @tag %></td> </tr> <% end -%> <% end -%> </tbody> </table>

    _update.html.erb

    路线

    <%= simple_form_for [object, tag], html: { class: 'form-horizontal', multipart: true },
        wrapper: :horizontal_form,
        wrapper_mappings: {
            check_boxes: :horizontal_radio_and_checkboxes,
            radio_buttons: :horizontal_radio_and_checkboxes,
            boolean: :horizontal_boolean
          } do |f| %>
    
      <div class="btn-toolbar btn-group" role="toolbar">
        <%= f.button :submit, 'Save', :class => "btn btn-xs btn-default" %>
      </div>
    
        <%= f.error_notification %>
    
      tag id = <%= @tag.id %>
    
        <%= f.input :content, placeholder: 'Tagged content'%>
    
        <%= f.association :tagtype, prompt: 'Select tag type', :collection => Tagtype.active.order(:name).where(:documenttype => @tag.tagable.documenttype_id) %>
    
        <%= f.input :location, prompt: 'add as: x1, y1, x2, y2' %>
    
    <% end -%>
    

    标记控制器

    Rails.application.routes.draw do
      root 'dashboard#index'
      devise_for :users
    
      concern :tagable do
        resources :tags, only: [:new, :index, :create, :edit, :update]
      end
    
      resources :users, :documenttypes, :tagtypes, :business_partners
      resources :tags, only: [:show, :edit, :destroy, :index]
    
      resources :documents do
        resources :comments
        resources :tags, concerns: :tagable
        get "pdf", on: :member 
      end
    
      resources :annotations do
        resources :comments
        resources :tags
        get "pdf", on: :member
    
    end
    
    get "annotations/:id/annotate" => "annotations#annotate", as: 'annotate'
    

    这就是我所做的 - 如果Rails中有更好的方法(我可以成像),那么更愿意学习。

1 个答案:

答案 0 :(得分:1)

此处采用的方法应该是使用嵌套表单,accepts_nested_attributes_forallow_destroy: truereject_if进行验证。

  • 要销毁嵌套记录,请使用accepts_nested_attributes_for :tags, allow_destroy: true并确保(除了:id):_destroy 被添加到strongparams
  • 使用嵌套字段的验证,使用 accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: :something_to_check