Generate form elements with scaffold_controller in Ruby on Rails - ruby-on-rails

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

Related

Rails adding a controller for an new table in the database

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)

In Rails, how do you generate the show view from an existing model?

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.

Rails: scaffold. Create admin controller for model

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

Rails Create and Edit on the same view

I am trying to accomplish Create and Edit on the same view (and same controller) on Rails 3.2 and Ruby 1.9.2. I have a partial and I am using form_for and the standard form helpers.
I submit using Ajax, so my page (form) is not refreshed or redirected. When the user tries to edit the form, naturally, Rails will create an entry instead of updating it. I was thinking of modifying the Controller's create method to detect existing entries, but I am not sure if this is the correct approach. Thanks.
You can pretty easily include the same form in both create and edit pages by storing it in a partial (typically _form.html.erb) and rendering the form in both create and edit.
This is what rails generate scaffold MODEL FIELD:TYPE FIELD1:TYPE1 will give you, as well.

Rails Routes - How to add a route of an object without a controller

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

Resources