简体   繁体   English

来自Ruby on Rails表单的用户输入

[英]User Input from Ruby on Rails form

Im trying to grab the users input value (:url) in my rails app. 我试图在我的rails应用程序中获取用户输入值(:url)。 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??? 如何获取user_input网址,以便可以将其传递到哈希中???

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

The params hash contains a mapping of the names of your HTML form controls to their values provided by the user. params哈希包含HTML表单控件的名称到用户提供的值的映射。 You access each one with params[key_name] . 您可以使用params[key_name]访问每个对象。

In your case, I think you want to create a local variable out of params[:url] . 就您而言,我想您想根据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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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