Monday 9 September 2013

Rails - Passing a parameter from one view to a different controller

Rails - Passing a parameter from one view to a different controller

Fairly simple question, and I've seen lots of similar ones asked in my
search, but none of the solutions I've read have worked so far. Not sure
where I'm going wrong.
In my application I have Projects and Verifications. When viewing a
particular Project, I need to be able to have a link to "Verify Project",
which will send the user to a new Verification form. They will enter a
variety of values, but I do not want them to have to select the Project
that this Verification belongs to by hand -- I want to be able to pass the
project_id directly alongside the values the user enters in the
Verification form.
On my project view I have:
<%= link_to "Verify Project", new_verification_path(:project_id =>
@project.id ) %>
and then in the verifications controller:
def new
@verification = Verification.new(params[:verification])
@project = params[:project_id]
end
def create
@verification = Verification.new(params[:verification])
@verification.project = @project
end
but this yields this error:
Validation failed: Project does not exist, Project does not exist
How can I create my Verification with :project_id being grabbed from the
previous page?
EDIT
From the Rails log: (clicking Verify Project)
Started GET "/verifications/new?project_id=4" for 127.0.0.1 at 2013-09-10
04:02:56 +0200 Processing by VerificationsController#new as HTML
Parameters: {"project_id"=>"4"} Rendered verifications/_form.html.erb
(91.3ms) Rendered verifications/new.html.erb within layouts/application
(97.5ms)
Rendered layouts/_shim.html.erb (0.3ms) Account Load (0.2ms) SELECT
"accounts".* FROM "accounts" WHERE "accounts"."remember_token" =
'NuxEYIjeCYyFklN7EyHTDQ' LIMIT 1 Rendered layouts/_header.html.erb
(38.6ms) Rendered layouts/_footer.html.erb (0.3ms) Completed 200 OK in
381ms (Views: 368.4ms | ActiveRecord: 2.7ms)
and then Create
Started POST "/verifications" for 127.0.0.1 at 2013-09-10 04:03:04 +0200
Processing by VerificationsController#create as HTML Parameters:
{"utf8"=>"&#10003;",
"authenticity_token"=>"sqUBXqA6y5oKCW1DFAi3sv8rQzm+tKjOYDdc/lvUS+c=",
"verification"=>{"verifier_name"=>"test", "checked_on(1i)"=>"2013",
"checked_on(2i)"=>"9", "checked_on(3i)"=>"10", "notes"=>"test"},
"commit"=>"Create Verification"} Account Load (0.2ms) SELECT "accounts".*
FROM "accounts" WHERE "accounts"."remember_token" =
'NuxEYIjeCYyFklN7EyHTDQ' LIMIT 1 (0.0ms) begin transaction
(0.1ms) rollback transaction Completed 422 Unprocessable Entity in 18ms
ActiveRecord::RecordInvalid (Validation failed: Project does not exist,
Project does not exist):
app/controllers/verifications_controller.rb:55:in `create'

No comments:

Post a Comment