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
Related
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.
I'm still trying to get my head around actions and routes. I more or less understand how to user forms with the build-in controller actions like create, show, etc. What I want to do for a demo app is imitate a school's class schedule, where I have Courses and Students with a has_and_belongs_to_many relationship.
I'm using Mongoid, and I can add students to a course and vice versa using the console, but I can't figure out how to do it with a form. Would adding students to a course even be a controller action, or can I write and call a setter in the model somehow? If a controller action is better, what would the route look like?
If anyone knows of an example that does something similar, I'd love to examine it.
Thanks
It can be a controller action. If adding students to a course is a simple logic, you could add /courses/:course_id/Students/add. This means creating a courses folder, and a students_controller within it, with an add action.
Example (in your routes.rb)
resources :courses, :except => [:destroy] do
resources :students
end
More info: https://gist.github.com/jhjguxin/3074080
Is this what you are looking for?
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
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.
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]