In rails, I have used migrations to make a new database table. It has an id, name, description, and a user_id (it has a foreign key to the user table).
I am very new to rails, and would like to know if there is a standard way to generate a controller for this new table.
This code will be added to an already existing/functioning site, so building it all with the traditional scaffolding tutorials doesn't seem to be the correct method.
This is a basic command to generate controllers
rails generate controller controller_name
You can create actions manually through editor or you can generate through command as well which is
rails generate controller controller_name index new
you can generate all actions after specifying controller name in the command
use this command
rails g controller ControllerName (Controller name must be singular)
Related
I have already a model in my Ruby on Rails aplication. I want to generate the Controller and Views.
Using the scaffold_controller option i get the files but the forms of the view dosnt contain the html input forms.
I can generate them using the parameters, for example
rails g scaffold_controller User name email
Is there some way to generate the html input tags without setting them as parameters? Can i make the scaffold_controller take the data from the existing model?
Thanks
Actually, when you use scaffold_controller, you are telling generator not to consider model. If you want to consider model attribites, you will need to pass the required attributes, as you are doing.
For more clarity:
Stubs out a scaffolded controller and its views. Pass the model name, either CamelCased or under_scored, and a list of views as arguments. The controller name is retrieved as a pluralized version of the model name.
To create a controller within a module, specify the model name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes
helper, template engine and test framework generators.
Another related post
Let's say you already have a model, but want to generate the show view that would normally be generated as if you generated that model via a scaffold. Is there a generator you can invoke to generate a show view from an existing model?
The command would look like:
rails g show_view User
I'd also want this to pick up the properties from my existing model and write them as fields into the views.
you need to use scaffold_controller
$ rails g scaffold_controller User
More info about scaffold_controller
Stubs out a scaffolded controller and its views. Pass the model name, either >CamelCased or under_scored, and a list of views as arguments. The controller name is >retrieved as a pluralized version of
the model name.To create a controller within a module, specify the model name as a path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes helper, template engine and test framework generators.
I want to create admin interface for existing model User.
What is the simplest way?
I think, it's scaffolding.
Scaffolding in rails is generated by script(rails generate scaffold), and there is no options to do it dynamically (like in django), correct?
So how can I create scaffolding controller AdminUsers for model User? Create AdminUsers scaffolding and replace AdminUser model by User?
What should I do if model will be changed by migrations? Manually update scaffolding controller and views, right? Is there any automation?
You can generate the files dynamically using scaffolding- the scaffold command can take a namespaced argument.
rails generate scaffold Admin::User
You don't need another model though- your Admin::UsersController and views should be working with the User model.
Using standard solutions such as activeadmin or rails_admin is the simpliest way. If you want something custom and still wondering how to scaffold controller AdminUsers for model User, you might want to take a look at rails-admin-scaffold gem which automates this process and an article with more detailed explanation.
ActiveAdmin is a neat user interface for your Rails application. It gives you a fully customizable user interface to your models.
Site: http://activeadmin.info/
Demo: http://demo.activeadmin.info/admin
Railscasts Episode: http://railscasts.com/episodes/284-active-admin
I just started trying to get acquainted with Rails, and I don't really know much Ruby. Currently I'm doing a beginners project to get acquainted with the framework which involves a simple form which takes a name, email, and phone number, and when you hit a submit button the page should refresh and present the information you submitted (so there is no database interaction and the model is supposed to do very little). It's very simple, but as my current knowledge of Ruby is pretty minimal, I'm getting somewhat confused. I've written the views for the most part but I'm still confused on what to put in the controller and the model. If anyone could provide any hints that would be great!
You need tableless model. Please refer excellent webcast from Ryan on this topic.
http://railscasts.com/episodes/193-tableless-model
Models (at least those descending from ActiveRecord::Base) require a database, so if you are using a model you are using a database, even if its a simple one like SQLite.
If you don't want to descend into models and generating a migration to create the tables for your models, then you are probably just going to store the form values into instance variables and reference those in your view. Below is a quick example how to do that.
If you have a form that sends data (ex: name and email) to a create action, the create action would look sort of like this:
def create
#name = params[:name]
#email = params[:email]
render :my_template
end
The create action above is assigning the params sent to it to instance variables, which you can then reference in your view. In the above example the create action is going to try and render a view called my_template which would probably be named my_template.html.erb and might look something like this:
<p>Your name is <%= #name => and your email is <%= #email =>.</p>
This is an extremely small and contrived example, but hopefully this helps.
When you move on to working with models your create action might instead create a new instance of a model, pass that model the params sent by the user, save the model, and then redirect them to a page that shows the data.
It is much easier with a database.
rails new stack
cd stack
rm public/index.html
rails generate scaffold Member name:string email:string phone:string
rake db:migrate
rails server
Then browse to localhost:3000/members
(Assuming you are using rails 3)
I created an object named settings. So i also provided its route in the routes.rb file i wrote "map.resources :settings". Now as i'm trying to save to the database with that object, it keeps on getting to the localhost:3000/settings url, which i don't have. i', also having this error
NameError in SettingsController#create
uninitialized constant SettingsController
PLEASE HELP! THANKS!
I am not 100% sure, but I believe you need to have a controller to add a route. Check out this diagram: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:mvc
If you use Rails, you have to stick to its rules. Rails implements the MVC pattern, where the controller has the role to provide the linking between a request started in the client web page (view) to creating, reading, updating and deleting (CRUD) objects (== models). The routes.rb define here the mapping from the URL to controller actions, not directly to the resources. See the "Rails Guides for Routing" for more information.
If you want to use your model objects, Rails provides an easy way to start that: scaffolding. By using rails generate scaffold setting <attr_name1>:<type1> ..., you are able to create the following:
A migration for the database that creates the settings table.
Generation of the model object Setting that maps to the created database table.
A controller SettingsController that allows CRUD for your model objects.
View files for the actions generated for the controller.
You can all do that by hand, but it is a good starting point to begin with. And read the basic tutorials and play with the example applications to get a feeling for Rails ...