在 Rails 中,您如何从 View 中获取信息以在 Controller 中使用。就像我有一个带有文本字段和按钮的页面一样,我如何将字段中的值(没有模型)发送到我的 Controller 以在我的一个函数中使用它。我正在使用 Rails 3
最佳答案
听起来你可以使用一个简单的形式,例如:
在您的views/products/index.html.erb
中:
<% form_tag omg_products_path do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>
在您的 controllers/products_controller.rb
中:
def omg
my_input = params['my_input']
#do whatever you want with my_input
end
您还需要配置 routes.rb
,例如:
resources :products do
post :omg, :on => :collection
end
关于ruby-on-rails - 从 View 中输入 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7772367/