简体   繁体   中英

Rails 4 Error: How to get parameters from URL

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM