简体   繁体   English

Rails 3/4:如何通过link_to增加和保存数据库值

[英]Rails 3/4: How to increment and save database value by link_to

I have a database with users, vendors, and accounts. 我有一个包含用户,供应商和帐户的数据库。

user has an account, account has an integer value credits 用户有一个帐户,帐户有一个整数值的点数

vendor has an integer value, product count 供应商具有整数值,产品计数

I want to be able to have a button on my app that when a button is clicked it will update these two items in the database: 我希望在我的应用程序上有一个按钮,当单击一个按钮时,它将更新数据库中的以下两项:

 current_user.account.credits
 vendor.product_count 

I am still vert new to rails and don't really know how it all works yet, so would love some help :D 我仍然对Rails感到陌生,还真的不知道它是如何工作的,所以希望获得一些帮助:D

My git repo on bit bucket so you can examine the code: https://umlungu@bitbucket.org/umlungu/mybeans.git 我在位存储桶上的git回购,因此您可以检查代码: https://umlungu@bitbucket.org/umlungu/mybeans.git

On the button_to, you supply parameters that get passed into the controller, and the controller operates based on the received parameters. 在button_to上,提供传递到控制器中的参数,然后控制器根据接收到的参数进行操作。 So far you've not given any information about what controller, what view, or whatever, so the answer is likewise general in nature. 到目前为止,您还没有提供有关什么控制器,什么视图或任何内容的任何信息,因此答案本质上同样是通用的。

In the view: 在视图中:

<%= button_to 'Something', {controller: "some_controller", action: "some_action", credits: credits, vendor_id: vendor.id} %>

In the controller "some_controller" 在控制器“ some_controller”中

  def some_action
    credits = params[:credits]
    credits ||= 0
    vendor_id = params[:vendor_id]

    # find the current user, and update its credits
    # find the vendor and update the product count?
    # send your user somewhere
  end
end

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

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