简体   繁体   中英

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. 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?

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". What should I do in another controller (lets call it "foods_controller.rb") to access the number. Maybe numbers.numa ?

First off, I recommend popping through the Ruby on Rails guide to 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. There are many other queries which can accomplished this way; check out the ActiveRecord::Base API doc for more info.

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. You can also call Number.first , Number.last or Number.find(<number_id>) if you only want a single record to be returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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