代码之家  ›  专栏  ›  技术社区  ›  Ben Strachan

带参数的简单表单上的自动完成字段

  •  0
  • Ben Strachan  · 技术社区  · 7 年前

    我有两个模型“活动模板”和“活动”。我想在活动模板索引上创建一个链接来创建一个新的活动。我要将活动模板参数发送到活动窗体。我想自动完成新活动窗体上的“活动模板”字段。

    我不知道如何在索引页上传递参数。

    我认为这是链接的问题。

    链接:

    <div class="box">
      <div class="box-header">
        <h3 class="box-title">Activities</h3>
        </div>
        <div class="box-body table-responsive no-padding">
          <table class="table table-bordered">
    
            <tbody>
              <% @activity_templates.each do |activity_template| %>
                <tr>
                  <td><a href="<%= new_app_activity_path(activity_template_id: @activity_template) %>"><%= activity_template.name %></a></td>
                </tr>
              <% end %>
            </tbody>
          </table>
          <div class="text-right">
            <%= will_paginate @activity_templates %>
          </div>
      </div>
    </div>
    

    网址:

    http://localhost:3000/app/activities/new
    

    <%= simple_form_for([:app, @activity]) do |f| %>
      <%= f.error_notification %>
    
      <div class="form-inputs">
        <div class="row">
          <div class="col-md-6">
            <%= f.association :resident, label_method: :full_name, prompt: "Choose a resident", collection: Resident.order(:first_name), selected: @resident.id %>
          </div>
          <div class="col-md-6">
            <%= f.association :activity_template, collection: ActivityTemplate.order(:name).pluck(:name, :id), prompt: "Choose an activity template", selected: @activity_template.id    %>
          </div>
          <div class="col-md-6">
            <%= f.input :published_status, collection: ['Draft', 'Final'], default: 'Draft' %>
          </div>
          <div class="col-md-6">
            <div class="form-group">
              <label>Author</label>
            <input class="form-control" type="text" placeholder="<%= @current_user.full_name %>" readonly value="<%= @activity.author.try(:full_name) %>">
            </div>
          </div>
        </div>
        <%= f.input :data %>
    
    
      </div>
    
      <div class="form-actions">
        <%= f.button :submit %>
      </div>
    <% end %>
    

    活动控制器:

      def new
        @activity = current_business.activities.new
        if params[:resident_id].present?
        @resident = Resident.find(params[:resident_id])
        end
        if params[:activity_template_id].present?
        @activity_template = ActivityTemplate.find(params[:activity_template_id])
        end
      end
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Pavan Cesar    7 年前

    我要将活动模板参数发送到活动窗体。

    你应该改变 @activity_template activity_template @activity_templates 作为

    <td><a href="<%= new_app_activity_path(activity_template_id: activity_template) %>"><%= activity_template.name %></a></td>
    

    现在你应该明白了 activity_template_id 价值观 url

    http://localhost:3000/app/activities/new?activity_template_id=value
    

    因为你已经捕捉到了 params[activity_template_id] new ,剩下的应该可以了。 link_to

    <%= link_to activity_template.name, new_app_activity_path(activity_template_id: activity_template) %>