Is calling rails g controller user multiple times safe? - ruby-on-rails

If I call
rails g controller user
multiple times to add actions, is this safe?
e.g. I did 'rails g controller user index' but now I want to create more actions?
btw, how can I create all REST based actions automatically?

yesh, safe. See rails g scaffold for generating REST actions automatically, including the model and views and tests.
Note that you can also pass a option --pretend when running the generator so that it shows you what files will be created, but does not actually create the files.

unless your generating a scaffold then you're probably off better doing it manually anyhow and not using the generator.
In your routes.rb make sure you've got
resources :user
so now all 7 restful routes will exist (you can check from terminal via rake routes) and then just add the methods to your controller as you need them, index, show, new, edit, create, update, delete. Don't forget if you don't want a route to exist you can omit them
resources :user, :except => [:index]
and vice versa if you only want a few methods
resources :user, :only => [:index, :create]

Related

ROR: Routes issue - Survey gem

The Survey gem is not creating the routes on my Rails app so I am wondering what to put in the routes.rb file?
I ran
rails generate survey:install
then ran
rails generate survey routes namespace:survey
and does not work.
I'm using Rails 4.2.1
The controllers are at controllers/survey/attempts_controller.rb and controllers/survey/surveys_controller.rb
The views are at survey/attempts/ and survey/surveys/
How should I put in the routes for these? Thanks.
Survey uses a standard CRUD interface, so you should be able to add resources named after the survey model you've created.
i.e. resources :surveys
If this doesn't do the trick, you can see a basic routing setup in the survey demo app here: https://github.com/runtimerevolution/survey-demo/blob/master/config/routes.rb
Let me know if this helps!
Its okay if it didn't add routes to your routes.rb file. You can add it yourself. And since you have it namespaced within Survey for your controllers you have to namespace in your routes file too.
namespace :survey do
resources :surveys
resources :attempts
get 'survey_details' => 'surveys#get_details' #this request will be handled by get_details method of SurveysController
end
Since it uses basic Create Read Update Delete in its controller, resources :surveys will take care of these. For any additional routes you want to define for your controllers make sure to put them inside the namespace block.

Remove the index, edit and show actions from my controller

I imagine this is fairly straight forward, but it's difficult to get the correct answer from google as delete/destroy an action is very similar to questions talking about the action: destroy()
I want to create a scaffold with just the create and destroy actions in my controller. I didn't use Nifty generator (which I think I will in the future).
My question is, what do I need to change in my application to fully remove the other actions? I'm talking about what changes in the routes file and other places, as well as the general files I'd need to delete such as views/modelname/show.erb
Thanks
I'll assume your model is a User
In routes.rb
resources :users, only: [:new, :destroy]
Delete everything other than new.html.erb from app/views/users
If you're going to use generators though it's probably quicker to do something like:
rails g model User name:string username:string email:string
rails g controller Users new destroy

RESTful actions specified in Users controller AND Devise?

So I'm running Rails 4, just made a barebones application and a User model with Devise. I also ran a migration to add a name attribute to the User model. I also generated a Users controller, as I want to be able to have a Users index page, where all Users are listed. If I specify resources :users in my routes.rb, then what do I do about methods like create in my Users controller, which are already handled by Devise? (I'm aware that this is a rather open-ended question, but some direction would be much appreciated.)
Thanks.
You only need the index action in your user controller.
You can do resources :users, :only => [:index] in your routes.rb file.

How to structure nested resources - Rails

In my Rails 4 app, an admin can moderate/approve posts, user accounts, and photos. I already have controllers for each of these (PostsController, UsersController, PhotosController) that handle the basic CRUD operations initiated by the user.
Having the update method for each controller seems incorrect and ... dirty. So does creating a single ModerationController with non-RESTful methods for each of the models.
I think I need something like a ModeratePostsController for each of the models, but I'm not sure how that gets scoped (under /admin?) or nested in routes.rb and generated as a controller.
Thoughts?
I was pretty much there.
I ran rails g controller Admin::Posts to create /app/controllers/admin/posts_controller.rb and in the routes file I added:
namespace :admin do
resources :teams
end

Using "resources" without scaffold in rails

I do not want to use scaffold in my rails app and there is something I do not quite get (yet!) regarding the "resources" keyword in the routes.rb.
Is the usage of "resources" linked to the generation with "scaffold" ?
My understanding is that "scaffold" will create a bunch of files, and among them a controller with the correct action's names (index, show, ...).
If I create the model (without using "scaffold") and then create a controller with the correct actions and the correct views, would this be sufficient to use "resources" in the routes.rb or will I miss something ?
Scaffold and resources is not linked in any way.
It's just that resources is already a kind of scaffold in that it always creates the CRUD routes that are also generated by the scaffold.
So if you write:
resources :users
You end up creating 6 routes for:
index
new
create
edit
update
destroy
You can limit what resources are generated by using :only
resources :users, :only => [:index, new]
Where only the index and new routes will be created.
You then can create those actions in your controller and add the appropriate views for them.
In short: If you just put resources :users in your routes.rb you can create these actions yourself in the controller and it will just work. No need to create a scaffold for it.

Resources