我有两个模型“ activity_templates”和“ activity”。我想在activity_template索引上创建一个链接以创建一个新的活动。我想将activity_template参数发送到活动表单。我想在新的活动表单上自动填写activity_template字段。
我不知道如何在索引页上传递参数。
我认为链接有问题。
链接:
<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>
URL:
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
答案 0 :(得分:0)
我想将activity_template参数发送到活动表单。
在将@activity_template
迭代为activity_template
时,应在链接中将@activity_templates
更改为activity_template
。
<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) %>