Mel Mel - 3 months ago 14
jQuery Question

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. 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) %>

Answer

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 alsouncheck 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.

Comments