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!

heroxav
  • 1,387
  • 1
  • 22
  • 65

2 Answers2

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.

Navin
  • 914
  • 8
  • 16
  • 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` – heroxav Sep 12 '16 at 11:24
  • This is the case where record is not present in your db/table. what you getting? when hitting params – Navin Sep 12 '16 at 11:28
  • 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 – heroxav Sep 12 '16 at 11:57
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.

Community
  • 1
  • 1
MKB
  • 777
  • 1
  • 9
  • 27