[英]How to access a table which created by a new “scaffold” in rails?
I use rails g scaffold to create a new group of controller, model and view, which allows users to input a int number. 我使用rails g脚手架创建了一组新的控制器,模型和视图,允许用户输入一个int编号。 They can only edit the only number but not create a new one or delete it.
他们只能编辑唯一的号码,而不能创建新号码或将其删除。
rails g scaffold number numa:integer
I would like to monitor what users input, and compare the number and call a function in another controller which I have already created before. 我想监视用户输入的内容,比较数字并在我之前已经创建的另一个控制器中调用一个函数。 However, I am quite new to rails on ruby, and do not have any idea about how to do that?
但是,我对使用ruby上的Rails还是很陌生,对如何做到这一点一无所知?
What I am pretty sure is that what users input will be stored in the a table which is bind with the new scaffold. 我很确定的是,用户输入的内容将存储在与新支架绑定的表中。 The table's name is 'numbers', and the only inputbox for the only number's name is "numa".
该表的名称为“数字”,唯一数字名称的唯一输入框为“ numa”。 What should I do in another controller (lets call it "foods_controller.rb") to access the number.
我应该在另一个控制器(让我们称之为“ foods_controller.rb”)中做什么来访问该号码。 Maybe
numbers.numa
? 也许
numbers.numa
?
First off, I recommend popping through the Ruby on Rails guide to ActiveRecord 首先,我建议通过Ruby on Rails指南弹出ActiveRecord
To specifically answer your question, a couple of different options: 为了专门回答您的问题,有两种不同的选择:
You can monitor the table contents through your model. 您可以通过模型监视表内容。 (More preferable, probably)
(可能更可取)
Try opening up a rails console ( rails console
, rails c
, or irb
), and typing in Number.all
, which will return a list of all Number records stored in the table. 尝试打开Rails控制台(
rails console
, rails c
或irb
),然后键入Number.all
,这将返回表中存储的所有Number记录的列表。 There are many other queries which can accomplished this way; 还有许多其他查询可以通过这种方式完成; check out the ActiveRecord::Base API doc for more info.
请查看ActiveRecord :: Base API文档以获取更多信息。
OR 要么
You can perform database queries directly on the database table you created. 您可以直接在创建的数据库表上执行数据库查询。
EDIT: Since you want to perform this in another controller, use the first method, calling Number.all
to get a list of all Number records stored in your database. 编辑:由于要在另一个控制器中执行此操作,请使用第一种方法,调用
Number.all
以获取存储在数据库中的所有Number记录的列表。 You can also call Number.first
, Number.last
or Number.find(<number_id>)
if you only want a single record to be returned. 如果只希望返回一条记录,也可以调用
Number.first
, Number.last
或Number.find(<number_id>)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.