How to prevent Rails controller generator to modify config/routes.rb
As of Rails 4.2, it's possible to disable route generation with the following code in your application.rb:
config.generators do |g|
g.skip_routes true
end
Source: https://github.com/rails/rails/commit/4b173b8ed90cb409c1cdfb922914b41b5e212cb6
Syntax to skip creating tests, assets & helpers for `rails generate controller`?
Try using --no-
followed by optionname
:
rails generate controller home index --no-helper --no-assets --no-controller-specs --no-view-specs
If you want to change the default behavior every time you run the generator command, you can configure the defaults you would like in the application.rb file - see How can I make sure Rails doesn't generate spec tests for views and helpers?.
Rails: Some index routes are showing /controller_name/index others are just showing /controller_name
When you create an action at the command line a route is automatically generated for it.
$ rails g controller ones index
create app/controllers/ones_controller.rb
route get 'ones/index' # route helper is ones_index
...
The behavior is totally agnostic of the action you're creating. It could be any action name and Rails will do the same thing
$ rails g controller twos smorgas
create app/controllers/twos_controller.rb
route get 'twos/smorgas' # route helper is twos_smorgas
...
When you add resources to your routes
resources :ones
you automatically get all of the default REST route helpers and routes, no matter how you create any of the REST actions.
$ rake routes
ones GET /ones(.:format) ones#index
POST /ones(.:format) ones#create
new_one GET /ones/new(.:format) ones#new
edit_one GET /ones/:id/edit(.:format) ones#edit
one GET /ones/:id(.:format) ones#show
PATCH /ones/:id(.:format) ones#update
PUT /ones/:id(.:format) ones#update
DELETE /ones/:id(.:format) ones#destroy
It's best to stick with Rails convention and use the default route helpers
ones_url
ones_path
# not ones_index_url
See this SO question if you want to disable the automatic route generation
What's the use of get routes rails generate controllers created in routes.rb in rails?
The 'get' is the HTTP verb supported by the routing engine. It's possible to route GET and POST (and the rest) to different methods, even if they hit the same URL. It's also possible to only support certain verbs for certain URLs (as is happening here).
Regarding their necessity - we'd have to see the rest of your routes.rb file to know. If you have a default match rule, that likely will take effect if these are removed.
Modify existing files with a rails generator?
Rather than modifying environment.rb, check out what you can do with Rails initializers. Basically, you're just going to create a new Ruby .rb file within config/initializers and keep your configuration loading code in there. If you need per-environment configuration, it's best to create another (usually YAML) file within config/ that will store the per-environment configuration variables and load that YAML in your initializer.
Can 'rails g controller' generate restful routes?
Try using rails generate scaffold_controller Foos
change routes of update rails
You can simply use <%= form_for @supply do |f| %>
for edit.html.erb file. Reason is: When you instantiate @supply
in edit
method in SuppliesController
, Rails will automatically post the form to update
method, you do not need to tell it explicitly. Same way, in new.html.erb, you will also use the same: <%= form_for @supply do |f| %>
, but now in your new
method, you will do @supply = Supply.new
, Rails will post this form to create method.
You do need to define routes, but as far as correct path is concerned, Rails will take care of it as long as you provide correct @supply
variable in form_for
.
Edit:
In your routes file:
resources :supplies
Stop Rails generating spec tests for views and helpers?
edit: short answer on top
If you want to do this for every time you run the generators, you do indeed customize it within your application.rb file. Put this code in the file inside the Application class definition.
config.generators do |g|
g.view_specs false
g.helper_specs false
end
You can also accomplish this by passing some options into the generator command. This Railscast goes over the process in more detail, but the basic idea is pretty simple.
Rails generators can take several options. You can see the options for the controller generator by running rails g controller -h
. Assuming you have Rspec set up already, if you look at the output, you notice a section that says "Rspec options". It looks like this:
Rspec options:
[--controller-specs] # Indicates when to generate controller specs
# Default: true
[--view-specs] # Indicates when to generate view specs
# Default: true
To negate these boolean values, just pass them in with a "no" in front of the name. So if you wanted a controller with no specs for your view, you would call it like this:
rails g controller Foobar index show new create --no-view-specs
And you would get a controller with the correct views and actions created for you, but no specs for your views.
The same thing applies if you are using the scaffold
generator. There is a --helper-specs
option, so if you wanted no view or helper specs you would run:
rails g scaffold Foobar name:string --no-helper-specs --no-view-specs
Related Topics
Rails 3 Caching: Expire Action for Named Route
How to Add Conditional Rubygem Requirements to a Gem Specification
Rails Error Message Displays Key and I Only Want Value
More Concise Version of Max/Min Without the Block
Ruby Looks for Class Variable in the Object Instead of Specific Class
Ruby Error After Installing Heroku Toolbelt
Why Does the Rand() Return Always the Same Number
Require Ruby File Without .Rb Extension
Are There More Elegant Ways to Prevent Negative Numbers in Ruby
Remove All Data from Active Storage
Interface Builder Not Seeing Outlets with MACruby
Problem Installing Ruby 1.9.2 on MAC Os Lion
Rvm Install Ruby-1.9.3-P286: Error Running 'Make -J 9' (Osx: Mountain Lion)
Get Instance Variable Name from Itself in Ruby
Compare the Content, Not the Results, of Procs
Have Delayed_Job Log "Puts", SQL Queries and Jobs Status
Why Does Hash#Select and Hash#Reject Pass a Key to a Unary Block