0

Im trying to grab the users input value (:url) in my rails app. I believe its

params[:user_input]

but i dont know how to implement it into my controller. Do I place it anywhere or in a specific method?

Method

 def index
 @posts = Post.all

 embedly_api = Embedly::API.new :key => '', :user_agent => 'Mozilla/5.0 (compatible; mytestapp/1.0; my@email.com)'
 url = params[:user_input]
 @obj = embedly_api.extract :url => :user_input
end

how can I get the user_input url so I can pass it into the hash???

2 Answers2

2

you should change your code to:

def index
   @posts = Post.all

   embedly_api = Embedly::API.new :key => '', :user_agent => 'Mozilla/5.0      (compatible; mytestapp/1.0; my@email.com)'
   @obj = embedly_api.extract :url => params[:user_input]
end
jack-nie
  • 736
  • 6
  • 21
0

The params hash contains a mapping of the names of your HTML form controls to their values provided by the user. You access each one with params[key_name].

In your case, I think you want to create a local variable out of params[:url]. That's fine. But then you use it like this.

def index
  @posts = Post.all
  embedly_api = Embedly::API.new :key => '', :user_agent => 'Mozilla/5.0 (compatible; mytestapp/1.0; my@email.com)'
  url = params[:url]
  @obj = embedly_api.extract :url => url
end

However, the local variable isn't necessary, so you could make the code slightly more succinct by removing the local variable and doing this instead:

@obj = embedly_api.extract :url => params[:url]

Vidya
  • 29,932
  • 7
  • 42
  • 70