开发者

rails, how to get params from url string? [duplicate]

This question already has answers here: Closed 11 years ago.

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜