1

I am fairly new to rails and I am struggling getting the id param out of this url:

http://localhost:3000/subscriberjobs/new?job_id=13

Currently I am using this line of code to get it:

@job = Job.find(params[:id])

This line directs to that:

redirect_to "/subscriberjobs/new?job_id=#{@job.id}"

Any help on how to achieve what I am trying to do is much appreciated!

2 Answers 2

1

try this, replace this line

@job = Job.find(params[:id])

with this line

@job = Job.find_by(id: params[:job_id])

and you will get your instance variable @job populated with the record which having job_id.

Sign up to request clarification or add additional context in comments.

3 Comments

Yep, that really should work. But when I am doin this I get this error: ActiveRecord::RecordNotFound in SubscriberjobsController#update Couldn't find Job with 'id'= so it somehow misses it's id in that case: 13
This is the case where record is not present in your db/table. what you getting? when hitting params
I think I know why this does not work. From my new_job form it jumps to this url: http://localhost:3000/subscriberjobs/new?job_id=13. Then after you done the payments it directs to this url: http://localhost:3000/subscriberjobs/1 (which does not work cause this model does not exist) and then checks for the job_id wich is obviously equal to nil
1

Did you try

url    = 'http://www.foo.com?id=4&empid=6'
uri    = URI.parse(url)
params = CGI.parse(uri.query)
# params is now {"id"=>["4"], "empid"=>["6"]}

id     = params['id'].first

You may check another post here for more description.

4 Comments

Hmmm... that spits out this error: NoMethodError in SubscriberjobsController#update undefined method split' for nil:NilClass`
Additionally it makes "id"=>"1"} out of every id
Then you need to check if the record exists in the database.
One thing though. Everytime it jumps to /subscriberjobs/1 after the payment via stripe. The record exists in Jobs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.