Reputation: 21
I've been looking for a solution to this for quite sometime now but I cannot wrap my head around on how to solve this. I'm new to Rails/Ruby programming and would appreciate any direction on this problem.
I have a form that takes in values and creates a Task object. Each Task belongs_to a Lead and a Lead has_many Tasks.
The path for a new Task takes in the lead_id
as one of the parameters.
/accounts/:account_id/reps/:rep_id/leads/:lead_id/tasks/new
But my goal is to also allow the user to use the form to create a Task for any lead. So, ideally, he should be allowed to select a lead from a dropdown and that lead_id needs to travel through to the CREATE method.
Here's my form:
<%= simple_form_for(@task, url: account_rep_lead_tasks_path) do |f| %>
<%= f.error_notification %>
<div class="field">
<div class="control">
<div class="select">
<%= f.collection_select :lead_id, @leads, :id, :name %>
</div>
</div>
</div>
<div class="field">
<div class="control">
<%= f.input :task_type, required:true, autofocus:true, input_html: {class: "input"}, wrapper: false, label_html: {class: "label"} %>
</div>
</div>
<div class="field">
<div class="control">
<%= f.input :description, required:true, autofocus:true, input_html: {class: "textarea"}, wrapper: false, label_html: {class: "label"} %>
</div>
</div>
<div class="field">
<div class="control">
<%= f.button :submit, class: "button is-primary" %>
</div>
</div>
By default, I render the new Task form for the latest Lead. Suppose the latest Lead created has an ID of 21, here's what shows in the URL:
http://localhost:3000/accounts/1/reps/2/leads/21/tasks/new
When I submit the form however, even though I'm selected a different lead via the collection_select input in the form for lead_id, the Task isn't being created for the selected Lead but being created for lead with id = 21.
The server logs show that the parameters that are being sent to the CREATE method are:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sKwctK492JH01yXilEI7jSSmv53ggiJuRBH/9Ate2AS+Lx53CO5BqgIOh44uvhFi6MPJ+Og1EY0U8m2BzqC9eA==", "task"=>{"lead_id"=>"16", "task_type"=>"Hello", "description"=>"Is it me you're looking for?"}, "commit"=>"Create Task", "account_id"=>"1", "rep_id"=>"2", "lead_id"=>"21"}
As you can see, there's a lead_id = 16 (which means that the form is taking the input correctly) but also another lead_id = 21 is present for which the Task is being created.
I want the Task to be created for lead_id = 16. Any idea on what I should be doing?
Here's my Task create controller action
def create
@account = current_account
@rep = current_account.reps.find(params[:rep_id])
@lead = @rep.leads.find(params[:lead_id])
@task = @lead.tasks.build(new_task_params)
if @task.save
redirect_to account_rep_lead_task_path(@rep.account_id,@rep.id,@lead.id,@task)
else
render 'new'
end
end
Upvotes: 0
Views: 57
Reputation: 1399
You are getting 2 different lead_id
because, first, you have url parameter lead_id
which is defined in your routes
:
/accounts/:account_id/reps/:rep_id/leads/:lead_id/tasks/new
And Second, it is from your form. So if you want to get lead_id
from your form, you can use:
params[:task][:lead_id]
else
params[:lead_id]
which is defined in your URL Parameter.
Upvotes: 0
Reputation: 5895
You'll find lead_id
under params[:task]
hash. Like: params[:task][:lead_id]
Upvotes: 0
Reputation: 7361
Try below code
def create
@account = current_account
@rep = current_account.reps.find(params[:rep_id])
@lead = @rep.leads.find(params[:task][:lead_id])
@task = @lead.tasks.build(new_task_params)
if @task.save
redirect_to account_rep_lead_task_path(@rep.account_id,@rep.id,@lead.id,@task)
else
render 'new'
end
end
Upvotes: 1