I have models in my Rails 4 app for Projects and Interests. 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.
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. If they click 'participate in this project', then the participant attribute in the interest table should be set to 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:
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
<%= 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
. To do so, replace your new
action with
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.