Ruby on Rails - 从输入框中获取值并传递到另一个页面 [英] Ruby on Rails - Getting value from input box and pass to another page

查看:63
本文介绍了Ruby on Rails - 从输入框中获取值并传递到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将输入字段的值传递到另一个页面,然后它会将这个值添加到数据库中.

I have to pass the value of an input field to another page where it will add this value to the database.

代码如下

    .shoping_cart_col2
      %input.cart_box{:type => "text", :value => "1"}/
    .shoping_cart_col3 
      = button_to "Add To Cart" , line_items_path(:product_id => @prod)

当我按下添加到购物车"按钮时,我希望将 cart_box 的值连同它一起提交到下一页,该值将作为 :quantity 传递到下一页

When i press the "Add to Cart" button i want the value of cart_box to be submitted to the next page with it, this value will be passed to the next page as :quantity

推荐答案

首先你需要输入字段的名称,以便 Rails 可以找到它并传递给 params 哈希.

First of all you need some name for input field, so Rails can find it and pass to params hash.

.shoping_cart_col2
  %input.cart_box{:name => 'item_amount', :type => "text", :value => "1"}

从现在开始,您可以在您的控制器中获取此值:

From now in your controller you can get this value with:

# Somewhere in ItemLinesController
def some_action
  ...
  @quantity = params[:item_amount].nil? ? 1 : params[:item_amount]
  ...
end

现在应该可以通过 @quantity 变量从下一页的视图访问它.

And now it should be accessible from next page's view via @quantity variable.

这篇关于Ruby on Rails - 从输入框中获取值并传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆