rails, how to get params from url string? [duplicate]
Possible Duplicate:
How do I easily parse a URL with parameters in a Rails test?
sorry for my english...
I have in my archives.rb model a method to get all src attributes from a html开发者_如何转开发 content, I am getting src's like:
http://localhost:3000/es/editor/archives/28/show_image?x=142&y=142
I need to get the params from that url, specifically: id, x, y
Thanks, Regards.
The correct approach :
url = "http://localhost:3000/es/editor/archives/28/show_image?x=142&y=142"
uri = URI::parse(url)
id = uri.path.split('/')[4]
params = CGI::parse(uri.query)
In your controller you can do 'params[:x]' and 'params[:y]'. For example:
x = params[:x]
y = params[:y]
It might be done using some regexp for example
irb(main):011:0> s = "http://localhost:3000/es/editor/archives/28/show_image?x=142&y=142"
=> "http://localhost:3000/es/editor/archives/28/show_image?x=142&y=142"
irb(main):012:0> s[/.*\/(\d+).+x=(\d+).*y=(\d+)/,1]
=> "28"
irb(main):013:0> s[/.*\/(\d+).+x=(\d+).*y=(\d+)/,2]
=> "142"
irb(main):014:0> s[/.*\/(\d+).+x=(\d+).*y=(\d+)/,3]
=> "142"
irb(main):015:0>
精彩评论