Rails - displays the view in the view options - pass

In my application, I can call view ('edit.html.erb') using:

<%= link_to( tag.content, [object, tag], method: :patch) %>

What would be the equivalent if I wanted to display it in a different view?

How do I pass parameters?

Edit

In essence, I have an idea of ​​a parent in which I want to list child elements (editable). I can also edit separately. A simple solution is that I create _partial, which I render from the parent view as well as from edit.html.erb.

I can name it edit.html.erb(or update) using the link:

<%= link_to( tag.content, [object, tag], method: :patch) %>

. , ( _update.html.erb) render? (.. ) () . , <% render partial "tags/update" PARAMS %>?

, () - () ().

/ "" PDF . / "", ( "link_to" ). "" 2 (. ):

  • : 2 - 1, () - 2, ( ).
  • : PDF

enter image description here

( )

_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'

class TagsController < ApplicationController

    def index
    @tags = Tag.all.order(:tagable)
    end

  def show
    tagable = detect_tagable
    @tag = tagable.tags.find(params[:id])
  end

  def edit
    tagable = detect_tagable
    @tag = tagable.tags.find(params[:id])
    @tag.update(tag_params)
  end

  def create
    tagable = detect_tagable
    tagable.tags.create(tag_params)
    redirect_to tagable_path(tagable)
  end

  def update
    tagable = detect_tagable
    @tag = tagable.tags.find(params[:id])
    render 'edit'
#    @tag.save
    @tag.update(tag_params)
  end

  def destroy
    tagable = detect_tagable
    @tag = tagable.tags.find(params[:id])
    @tag.destroy
    redirect_to tagable_path(tagable)
  end

  private

    def tagable_path(tagable)
      case tagable
      when Document
        document_path(tagable)
      when Annotation
        annotate_path(tagable)
      else
        fail 'Unknown tagable'
      end
    end

    def detect_tagable
      if params[:annotation_id]
        Annotation.find(params[:annotation_id])
      elsif params[:document_id]
        Document.find(params[:document_id])
      else
        fail 'Tagable not found'
      end
    end

    def tag_params
      params.require(:tag).permit(:content, :location, :tagtype_id, annotation_attributes: { annotation_ids:[] }, document_attributes: { document_ids:[] })
    end

end

, - Rails ( ) .

+4
1

, , accepts_nested_attributes_for allow_destroy: true reject_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
+1

Source: https://habr.com/ru/post/1658015/


All Articles