Rails 3: Adding paths in routes for new controller? - ruby-on-rails

I created a new controller for one of my models called Review, and named it review_controller, i put in show and update methods in it but cant get them working because rails didn't add the paths for the 2 methods.
I tried putting the follwoing in the routes file: match "/review/update/:id", :to => "review#update"
But it gives me a ActiveRecord::RecordNotFound (Couldn't find Review with ID=update):
app/controllers/review_controller.rb:16:in `update'
how can i add the path to the routes file to make my update and show methods work?
Thank You

For a new controller in Rails 3, you can let Rails build the routes for you like this:
resources :review, :only => [:show, :update]
And then if you were to run rake routes in the Terminal, you'd see:
review GET /review/:id(.:format) {:action=>"show", :controller=>"review"}
PUT /review/:id(.:format) {:action=>"update", :controller=>"review"}

Related

Correct way to generate idiomatic rails controller?

I'm learning rails 6 and walking through creating some toy resources. I know in my data model, I want to create a "widget" let's say, but I don't want everything that a scaffold is going to produce, so I'd like to manually generate everything that I know I'll need and nothing more. I started with a simple model. Next I'm trying to generate the controller + views I know I'll need. I ran:
rails g controller widgets index new show delete
This ended up creating a router with the following routes:
get 'widgets/index'
get 'widgets/new'
get 'widgets/show'
get 'widgets/delete'
I'm surprised by this, I would have expected something similar to the routes that are generated by default with the scaffold:
things GET /things(.:format)
POST /things(.:format)
new_thing GET /things/new(.:format)
edit_thing GET /things/:id/edit(.:format)
thing GET /things/:id(.:format)
PATCH /things/:id(.:format)
PUT /things/:id(.:format)
DELETE /things/:id(.:format)
Am I using the correct generator command to accomplish what I described? Or is this something that I would just need to manually set up with my routes?
EDIT: I realized the action I'm actually looking for is :destroy, and not :delete
This is normal if you don't want to use scaffold, and for some routes they are need to be setup manually like the delete route, it is a delete request not get like the controller is generating (the controller always generate get requests because it expects a corresponding view)
You can use: resources :widgets, only: [:index, :new, :show, :destroy]
this will generate what you want because this is the convention used in rails
The Rails generators are really just for generating the needed files, for anything out of the ordinary you will have to manually change things.
You can add this to your routes.rb instead of that list of GETs
resources :widgets, only: [:index, :new, :show, :destroy]
Which will produce an output similar to what you have in your question if you do rails routes from the terminal.

No route matches although route is made

I am following the following tutorial, https://www.dailysmarty.com/posts/how-to-add-search-functionality-into-a-rails-api-application .
After managing to complete the entire tutorial, I realised that there are no route that matches "/search". The tutorial did not state how to do routes, hence I have attempted to do one myself by creating the following:
Rails.application.routes.draw do
resources :search, only: [:search]
end
This is not what you want. You need a get route like this:
get '/your_route', to: 'your_controller#your_action'
So, in your case, search is not a resource. So I would use:
get '/search', to: 'search#search'
You can find all this info regarding routing on the rails guide.
I hope this helps!
Update:
As per the tutorial you need to visit yoururl.com/search
In that case, it's better to use the solution provided by #jeremie
get '/search', to: 'search#search'
This will generate
search GET /search(.:format) search#search
The format you are using is incorrect.
only: supports only the seven default actions ->
index, show, new, create, edit, update, and destroy more...
For the custom routes you need to change it to the following
Rails.application.routes.draw do
resources :search, only: [] do
get :search, on: :collection
end
end
This will generate the following route
search_search_index GET /search/search(.:format) search#search

Ruby on Rails using a POST method

I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
What does this do, and where should I put it? I tried putting it in the "create" method that follows this form:
http://dixonc3-72812.use1.nitrousbox.com/posts/new
I also tried putting it in the "view" because I figured it was accepting the "POST" method. How do I proceed? New to Ruby on Rails and trying to figure out the kinks.
I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
Section 5.7 doesn't tell you that. What it tells you is the following is output of rake routes for the show action:
post GET /posts/:id(.:format) posts#show
The output is presented in a tabular format where the four columns are (from left to right): Prefix, Verb, URI Pattern, Controller#Action.
In order to get this, you need to declare posts as a resource in config/routes.rb as:
resources :posts, only: [:show]
Now if you run rake routes in your terminal, you will see the line included from the guide.
The above line in config/routes.rb defines a route for the show action. You can remove only: [ :show ] option to have routes defined for all standard RESTful actions, i.e. in your config/routes.rb:
resources :posts
This is the routes for show action of posts controller, for create action you find something like
posts#create

Simple Navigation Routing Error in Rails 3.2.8

longtime reader & first time poster, so I'd appreciate it if you went easy on me.
Recently started teaching myself RoR, and have been hacking away at a personal project/website to get the hang of things. Here's my problem:
I'm using the Simple Navigation gem to generate links. Inside navigation.rb I'm trying to call:
primary.item :home, 'Home', home_path
...where home is a view and controller that displays my front page:
home > index.html.erb (just contains a bunch of standard HTML, but let me know if it'd be useful to include)
and controllers > home_controller.rb:
class HomeController < ApplicationController
def index
#posts = Post.all
end
end
I'm getting this error, though:
Routing Error
No route matches {:action=>"show", :controller=>"home"}
Try running rake routes for more information on available routes.
... so I run rake routes, and can definitely see "home#show" in there.
My routes.rb file, as well, has this in it:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index"
resources :posts
resources :home
So I'm a little baffled, and I'm sure it's because of my inexperience or general inability to understand what I'm doing, but I'd really appreciate some help as it's more or less a road block that I haven't been able to overcome.
Appreciate it!
Jay
It is because of the resources; if you are not using the resources remove resources :home
This could be your routes:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index", :as => "home"
resources :posts
See how I removed the resources :home. In home/index the :as represents an alias, so you can use the alias as a method, adding "path" at the end of the name.
Check this guide about routes and resources: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

Custom actions in rails controller with restful actions?

I'm having some trouble with using creating my own actions inside a controller I generated using the scaffold.
I understand everything maps to the restful actions but I'm building a user controller where users can login/logout, etc but when I created the action and declared it in the routes.rb I get this error when I visit users/login
Couldn't find User with id=login
It tries to use login as a ID parameter instead of using it as an action.
Routes.rb
match 'users/login' => 'users#login'
I think I'm doing something wrong with the routes so if anybody could help me that would be great.
Thanks
I assume your routes.rb looks like this:
resources :users
match 'users/login' => 'users#login'
The problem is that Rails uses the first route that matches. From the documentation:
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action’s route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
So either define your custom route before resources :users:
match 'users/login' => 'users#login'
resources :users
…or use this syntax for adding more RESTful actions:
resources :users do
collection do
match 'login'
end
end
To see the existing routes (and their order) run rake routes from the command line.

Resources