Rails 4 - 简单表单:如何根据用于访问表单的 link 填充表单字段?

Rails 4 - simple form: how do I populate a form field based on the link used to access the form?

我的 Rails 4 项目和兴趣应用程序中有模型。这些协会是:

项目

has_many :interests

兴趣

belongs_to :project
belongs to: user

意向表的目的是让用户注册对项目的兴趣。

在我的项目展示视图中,我有两个 link 可以注册兴趣。用户可以作为参与者或观察员表达对项目的兴趣。

在我的兴趣表中,我有两个布尔字段,分别称为 :participant 和 :observer。

我正在尝试找出如何使用 'true' 填充相关的布尔属性,具体取决于用户单击哪个 link 以进入表单。如果他们单击 'participate in this project',则兴趣 table 中的参与者属性应设置为 true。

在我的兴趣表中,我有:

<%= f.hidden_field :user_id, @current_user.id %>
<%= f.hidden_field :project_id, @project.id %>

<%= f.input :observe, as: :radio_buttons, :label => "Do you want to monitor this project?" %>
<%= f.input :participate, as: :radio_buttons, :label => "Do you want to participate in this project?" %>

有没有一种方法可以让我自动完成此表单中的观察和参与布尔值,link 用户根据该表单从项目展示中访问此表单?

兴趣路线(在我的应用中它实际上被称为'eoi':

rake routes | grep eoi
                        eois GET       /eois(.:format)                                             eois#index
                             POST      /eois(.:format)                                             eois#create
                     new_eoi GET       /eois/new(.:format)                                         eois#new
                    edit_eoi GET       /eois/:id/edit(.:format)                                    eois#edit
                         eoi GET       /eois/:id(.:format)                                         eois#show
                             PATCH     /eois/:id(.:format)                                         eois#update
                             PUT       /eois/:id(.:format)                                         eois#update
                             DELETE    /eois/:id(.:format)                                         eois#destroy

在项目展示视图中,我有这些 link 可以注册参与或监督项目的兴趣:

<%= link_to 'Participate', new_eoi_path(@eoi) %>
<%= link_to 'Monitor', new_eoi_path(@eoi) %>

无论是观察者还是参与表单的查询字符串都可以传入

将转到此表单的路由助手更改为

new_eoi_path(observe: true)

new_eoi_path(participate: true)

然后将新视图中的输入更改为

<%= f.input :observe, checked: params[:observe],as: :radio_buttons, :label => "Do you want to monitor this project?" %>
<%= f.input :participate, checked: params[:participate], as: :radio_buttons, :label => "Do you want to participate in this project?" %>

编辑

使用这个作为输入怎么样?

<%= f.radio_button :observe, true, :checked => params[:observe] %>
<%= f.radio_button :participate, true, :checked => params[:participate] %>

将此添加到您的视图中

<%= link_to 'Participate', new_task_path(@eoi, participate: true) %>

<%= link_to 'Monitor', new_task_path(@eoi, observe: true) %>

在你的控制器中,

def new
  @task = Task.new
  @participate = params[:participate] # will be nil if observe link was clicked
  @observe = params[:observe]
end

最后在表格中,将 checked 设置为基于 @observe@participate

的值
<%= f.input :observe, checked: @observe, as: :radio_buttons, :label => "Do you want to monitor this project?" %>
<%= f.input :participate, checked: @participate, as: :radio_buttons, :label => "Do you want to participate in this project?" %>

额外

如果用户点击 observe link,您需要检查 observeuncheck participate。为此,请将您的 new 操作替换为

def new
  @task = Task.new
  @participate = params[:participate] || false
  @observe = params[:observe] || false
end

这样,如果用户点击 observe link,yes 的值将设置为 observeno 将设置participate.