简体   繁体   English

Rails 3/4如何使用Rails脚手架.update

[英]Rails 3/4 How to use rails scaffold .update with a button

I used rails generate scaffold vendor expresso_count:integer 我用rails生成脚手架供应商expresso_count:integer

I now want to make a button that when clicked updates expresso_count for that vendor from the show.html.erb view that gets generated 现在,我想创建一个按钮,当单击该按钮时,会从生成的show.html.erb视图更新该供应商的expresso_count

But I have two problems. 但是我有两个问题。

problem A) when using this code, it works fine and will update: 问题A)使用此代码时,它工作正常,并将更新:

<% @vendor.update(expresso_count: '10' ) %>

but this code does not, and its what I want, so that I can just increment the expresso_count by 1. What it current does is just set the value to 0e 但是此代码没有,而且它是我想要的,因此我可以将expresso_count递增1。它当前所做的只是将值设置为0e

<% @vendor.update(expresso_count: '@vendor.expresso_count+1' ) %>

Problem B) How do I put that into a button_to such that I can have a button which when pressed, will increment @vendor.expresso_count by 1 问题B)如何将其放入button_to中,以便我可以拥有一个按钮,当按下该按钮时,@ vendor.expresso_count会增加1

Here is a link to my git repo (which will be a bit out of date) https://umlungu@bitbucket.org/umlungu/mybeans.git 这是我的git 仓库的链接(将会有点过时) https://umlungu@bitbucket.org/umlungu/mybeans.git

I'm still very new to Rails and learning as I go. 我仍然对Rails和学习感到很新。 so would appreciate any help. 因此,将不胜感激。

Thanks :D 感谢:D

Firstly, let me heartily recommend Michael Hartl's rails tutorial here: https://www.railstutorial.org/ - I'm also new to Rails and it is excellent. 首先,让我在这里衷心推荐Michael Hartl的rails教程: https : //www.railstutorial.org/-我还是Rails的新手,它很棒。

The first thing to understand is the MVC framework that rails uses. 首先要了解的是Rails使用的MVC框架。 Models hold you data, Views are how you interact with your data, Controllers are the glue between them. 模型保存您的数据,视图是您与数据交互的方式,控制器是它们之间的粘合剂。

So you want a view that is going to make your controller do something to your model. 因此,您需要一个视图,该视图将使您的控制器对模型执行某些操作。 Currently you're trying to do this from a view. 当前,您正在尝试从视图中执行此操作。

I would put a link in a view to /vendor/#{@vendor.id} and POST some kind of data indicating that I am wanting to increment my expresso_count. 我会把在视图中的链接/vendor/#{@vendor.id}POST某些类型的数据表明,我想我的递增expresso_count。 In my view, I would have something like: 在我看来,我会有类似的东西:

def update
  #assuming all I do is increment my count
  Vendor.find(param[:id]).expresso_count += 1
end

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

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