0

I am currently using the script

 <?php $name = $_GET ["id"]; ?>

to pull the parameter from a URL like www.example.com?id=john and then using

 <?php echo $name ?> 

when I need the variable in the code.

I now need to use Ruby instead of PHP to accomplish this task. Are there any comparable methods in Ruby on Rails?

5
  • If you set up your Rails application properly, they would be passed in via class instance variables set in the controller. The controller gets them from the params hash. For a Rails app, you don't just replace php with Ruby, if that's what you're attempting. See Action Controller Overview. Commented Jul 14, 2014 at 17:11
  • I am making a web app that opens in a separate window and is not contained in the rails framework, I just access it in the public folder. The website manager does not want me using PHP however due to possible security vulnerabilities and wants me to replicate what the php does with GET using a Ruby script. I am not familiar with ruby scripts though. Commented Jul 14, 2014 at 17:15
  • Your problem said "Rails" which is a complete web application framework with classes, etc, defined to take care of all this. You should remove "rails" from the reference (subject and tags) if you're not using Rails and are just using Ruby. Commented Jul 14, 2014 at 17:16
  • Okay done. I am integrating this app into a website that uses ruby on rails but I have developed my app outside of the framework and just need a script that can bridge the data through url parameters. Commented Jul 14, 2014 at 17:19
  • I found something related here, maybe this helps? Specifically, decode_www_form. Commented Jul 14, 2014 at 17:21

1 Answer 1

1

Rails has all the functionality needed, but it's a steep learning curve, so maybe Sinatra would be better for what you need. In particular, Sinatra, like Rails, makes it easy to get at the parameters in a URL. See Sinatra's "Routes" documentation if you want to go that way.

Here's code to extract the parameters from a URL using Ruby and its bundled URI class:

require 'uri'
uri = URI.parse('http://www.example.com?id=john')
uri.query # => "id=john"
Hash[URI.decode_www_form(uri.query)] # => {"id"=>"john"}

You can assign the result of the last line to a variable, then use it to access any of the parameter's values via its name:

params = Hash[URI.decode_www_form(uri.query)]
params['id'] # => "john"
Sign up to request clarification or add additional context in comments.

Comments

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.