Custom route for 'news' resource - ruby-on-rails

I have a resource in a Rails 3.2 app called "news". I know it is not the best name to use for a resource but I'd prefer not to change it.
I have an issue with the routing since new_news_path doesn't work.
How can I define a custom route for the new action? something like unused_news_path for news#new?
thanks for your help.

Try this in your routes.rb:
match "unused" => "news#new"
When you do a rake routes | grep unused, you will get the following output:
unused /unused(.:format) news#new
So you now can use unused_path on your views and controller to get to the correspondent action.

Related

How can I take out the first segment of a route in rails 4 scaffold

I am trying to create a scaffold called 'Pages'.
So far everything is fine but the page structure now needs to change where it currently is:
http://0.0.0.0:3000/pages/the-page-name
What I need to do now is have this instead:
http://0.0.0.0:3000/the-page-name
In my routes.rb I have this:
resources :pages
This obviously maps all routes within the model to this base but I want to hide this.
Is it entirely possible?
Thanks,
Taken from the Rails Routing Guide, you could do this:
get '*pages', to: 'pages#show', format: false
I would recommend you make it the very last route you have, since the Rails router matches the request with the first route, and having a wildcard early in your routes file will end up clobbering all your other routes/resources.
You can specify a path
resources :pages, path: ''

Rails routing error when using resource but now when using GET

I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)
"Routing Error
No route matches [GET] "/suggestion-boxes"
In my routes.rb file I have:
SuggestionBoxApp::Application.routes.draw do
resources :suggestion_boxes
end
This is what I get when I run rake routes:
suggestion-box-app$ rake routes
suggestion_boxes GET /suggestion_boxes(.:format) suggestion_boxes#index
POST /suggestion_boxes(.:format) suggestion_boxes#create
new_suggestion_box GET /suggestion_boxes/new(.:format) suggestion_boxes#new
edit_suggestion_box GET /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
suggestion_box GET /suggestion_boxes/:id(.:format) suggestion_boxes#show
PUT /suggestion_boxes/:id(.:format) suggestion_boxes#update
DELETE /suggestion_boxes/:id(.:format) suggestion_boxes#destroy
However, if I modify my routes file to
SuggestionBoxApp::Application.routes.draw do
get "suggestion-boxes" => "suggestion_boxes#index"
end
Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.
I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.
Any insights would be very much appreciated.
The error is that you are not renaming the REST route, instead the controller one.
Try declaring
resources :suggestion_boxes, :as => "suggestion-boxes"
in your config/routes.rb file.
Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

Rails Routing and View Helper Names

I have this route defined in my routes.rb:
matc "articles/:id/vote/up" => "articles#vote", :liked=>true
The rake for this route is:
/articles/:id/vote/up(.:format) jokes#vote {:liked=>true}
With the first column being blank.
My question is, what would the route helper name be for this link?
For example articles_vote_up_path(:id).
Is there an uglier way to link to this path?
How can I find out?
I'm thinking I misunderstood something, please guide me in the correct direction in that case.
Thank You
you need to pass an :as option to use a named route
match "articles/:id/vote/up" => "articles#vote", :liked=>true, as: :article_vote_up
which generates an article_vote_up_path
When you run rake routes, you can add _path to the whatever's in the first column on the left, and that's what the route url helper is for that link. For example, here's a line from rake routes on one of my projects:
subscriptions GET /subscriptions(.:format) subscriptions#index
The url helper for this url would be subscriptions_path.

catching wild card route for missing routes, doesn't work when having additional routes files

I implemented a simple custom errors solution.
this one: http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
everyhing is working fine except the missing routes in the routes.rb file..
in order to get to my error_controller when there is a missing route i did the wildcard solution: match '*not_found', to: 'errors#error_404'
but... now when i try to enter a sub section of my site which seats under:
/admin, i get to the error page. the wilcard gets triggered, even tough the route for admin section is defined in a different route file, under: config/routes/admin.rb
what can I do?
thanks
edit:
using rails 3.0.20 and ruby 1.8.7
If you're using Rails 3.2+, there is a simpler solution for your routes. First in 'config/application.rb' set your app as the error handler
config.exceptions_app = self.routes
Now when there is an your app will look to your routes to handle it. In 'config/routes.rb' you can add a route such as:
match "/404", :to => "errors#not_found"
A more verbose explanation can be found here.
OK so until I will update to Rails 3.2+
I simply put '*not_found', to: 'errors#error_404' into the last route file that is loaded.
that way its truly in the end of the routes and now all my routes work. and the error is still fired when needed.

Newbie Question:Have to type localhost/controller/index in browser to get index method to display in Rails. Why?

I am new to Rails and I am reading Sitepoint's Simply Rails 2 but I am using Rails 3.0.0. In the book we had just set up our first controller (StoriesController) and fired up the server and typed in http://localhost:3000/stories and it should have displayed the index.html.erb file but instead when I type in that url I get "Routing Error: No route matches "/stories" but when I type in http://localhost:3000/stories/index it works properly. Can somebody explain to me why rails is not loading the index.html.erb file implicitly when I go to localhost/stories?
Depending on how you created your routes (in config\routes.rb). Unfortunately, if you scaffold a controller, rails now generates a route like this:
get 'posts#index'
If it is a restful-controller, you better write
resources :posts
Or if it is a special controller (with only an index action) you could write
match '/posts' => 'posts#index'
To provide the fallback match ':controller(/action(/:id(.:format))) is generally avoided. Because it opens up all your controller-methods. The preferred way is that you declare explicitly how to access your site.

Resources