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.
Related
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.
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 have the following routes defining certain resources:
resource :account, :only => [:show]
namespace :account do
resource :billing
end
So, I have an AccountsController which generates the "show" page at /account.
I also have a BillingsController which I want to be viewed at /account/billing.
This is working fine, but one thing that's bugging me is the convention says the view folder for the AccountsController is plural even though its a singular resource -- not a big deal, but when creating a matching namespace for the nested resource I now have two seperate view folders -- /app/views/account(for namespace) and app/views/accounts(for account resource).
So, this kind of throws me off.
What would be the best way to make the AccountsController use the singular account folder for views?
For what it's worth, I decided it would be easiest to change AccountsController to AccountController and change my routes.rb file like so:
resource :account, :controller => 'account'
That was a little cleaner than anything I could figure out.
There are a bunch of options, but using self.prepend_view_path("views/account") in a method called by a before_filter in the AccountsController should work. It will force it to look in the specified directory for a view, before checking the default.
I'm just upgrading my app to Rails 3 and as I have to rewrite my routing anyway, I'm taking some time to improve my named routes.
I have an invoices controller which has a trash action (/invoices/trash lists all invoices in trash). I want to access this through a named route (i.e. trash_url) for simplicity in my views.
I can achieve this easily enough with the following
match "/invoices/trash" => "invoices#trash", :as => :trash
What I want to know is if there is a way of doing this within the block where I define the routes for my invoice controller. I have tried the following and it doesn't work.
resources :invoices do
collection do
get :trash, :as => :trash
end
end
Is what I am trying to do possible or do I have to define my named route outside of this block?
Thanks.
The method you list (shown below) works fine for me, it generates trash_invoices_path and trash_invoices_url helper methods.
resources :invoices do
collection do
get :trash, :as => :trash
end
end
You can make methods in your application controller named trash_url and trash_path that just call and return the path from the generated methods mentioned above if you have a need to use those specific method names instead of the generated ones.
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]