简体   繁体   English

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

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

I have models in my Rails 4 app for Projects and Interests. 我在Rails 4应用程序中有针对项目和兴趣的模型。 The associations are: 关联是:

Project 项目

has_many :interests

Interest 利益

belongs_to :project
belongs to: user

The purpose of the interest form is for users to register interest in a project. 兴趣表的目的是让用户注册对项目的兴趣。

In my projects show view, I have two links to register interest. 在我的项目展示视图中,我有两个链接来表示兴趣。 Users can express interest in a project either as a participant or as an observer. 用户可以以参与者或观察者的身份表达对项目的兴趣。

In my interest form, I have two boolean fields called :participant and :observer. 以我的兴趣形式,我有两个布尔字段,分别是:participant和:observer。

I'm trying to figure out how to populate the relevant boolean attribute with 'true', depending on which link the user clicks to get to the form. 我试图弄清楚如何用'true'填充相关的布尔属性,具体取决于用户单击该链接以获取表单的链接。 If they click 'participate in this project', then the participant attribute in the interest table should be set to true. 如果他们单击“参与此项目”,那么兴趣表中的参与者属性应设置为true。

In my interest form, I have: 根据我的兴趣表,我有:

<%= 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?" %>

Is there a way that I can auto-complete the observe and participate booleans in this form, based on which link the user follows to access this form, from the projects show? 有没有一种方法可以让我自动完成观察并以这种形式参与布尔值(基于用户从显示的项目中访问该表单所遵循的链接)?

Routes for interests (it's actually called 'eoi' in my app: 兴趣路线(在我的应用中实际上称为“ 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

In the projects show view, I have these links to register interest to participate or monitor a project: 在项目展示视图中,我具有以下链接,以注册参与或监视项目的兴趣:

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

You can pass in whether it is observer or participate in the query string of the form. 您可以传递它是观察者还是参与表单的查询字符串。

Change the route helper that goes to this form to 将转到此表单的路由助手更改为

new_eoi_path(observe: true)

or 要么

new_eoi_path(participate: true)

And then change your inputs in the new view to 然后在新视图中将输入更改为

<%= 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?" %>

Edit 编辑

How about using this for the inputs? 如何将此用作输入?

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

Add this to your view 将此添加到您的视图

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

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

And in your controller, 在您的控制器中

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

And finally in the forms, set checked to the value based on @observe or @participate 最后在表单中,将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?" %>

EXTRA 额外

If the user clicks on the observe link, you would want to check observe and also uncheck participate . 如果用户单击observe链接,您将要检查observeuncheck participate To do so, replace your new action with 为此,请将您的new操作替换为

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

This way, if the user clicks on the observe link, the value of yes will be set for observe and no will set for participate . 这样,如果用户单击observe链接,则将值yes设置为observe而将no设置为participate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM