i am rewritting a rails app I did some 5 years ago using rails 1.something.
When I try to browse localhost/companies/search_updates/ I get this error... I know this is a routing error because when I remove the resources :companies from router.rb the thing works fine... How can this be fixed? And do I need to manually add routes for every action I create?
Error when I try to access localhost/companies/search_updates/
The action 'show' could not be found for CompaniesController
Controler
class CompaniesController < ApplicationController
def index
#companies = Company.all
end
def search_updates
# Execute code to search for updates
# Redirect to results
end
end
Routes
resources :accounts
resources :companies
get 'companies/search_updates' => 'companies#search_updates'
search_updates.html.erb
Hello Updates!
The action 'show' could not be found for CompaniesController
Rails routes are matched in the order they are specified, so if you
have a resources :companies above a get 'companies/search_updates' 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.
resources :accounts
get 'companies/search_updates' => 'companies#search_updates'
resources :companies
And
Resource routing allows you to quickly declare all of the common
routes for a given resourceful controller. Instead of declaring
separate routes for your index, show, new, edit, create, update and
destroy actions, a resourceful route declares them in a single line of
code.
If you have only index method (default CURD rails) on your controller, you can specific routes for it.
resources :accounts
get 'companies/search_updates' => 'companies#search_updates'
resources :companies, :only => [index]
Related
I'm just learning Ruby on Rails and building a fairly simple app that stores info to a rake generated db. I'm trying to add a new action to the controller but I can't trigger it. There's a before_action getting triggered which shouldn't be.
class GamesController < ApplicationController
before_action :set_entry, only: %i[ show edit update destroy ]
before_action :authenticate_user!
def index
end
def analyse
#The function here doesnt matter, its not reaching it because its hitting the set_entry instead
puts 'howdy'
end
private
def set_entry
#entry = Entry.find(params[:id])
end
end
The error I'm hitting is Couldn't find Game with 'id'=analyse", highlighting the set_entry action, which to my understanding should only be running with the show, edit, update, and destroy actions (which are also in the full controller and running fine). There are other actions in the controller (such as create and new) which don't seem to trigger that set_entry and are running just fine as expected. For example, linking to the new path takes me to /entry/new, while linking to an edit path takes me to /entry/:id/edit, which is all fine. But it keeps linking my new analyse action trying for entry/:id/analyse when I want it to go to entry/analyse.
My button to trigger it is simply:
<%= link_to "Analyse", analyse_path %>
Which is in a navbar in my application.html.erb
And here's my routes.rb:
Rails.application.routes.draw do
resources :entry
devise_for :users
resources :users
root to: "entry#index"
get 'entry/index'
get 'entry/analyse', as: 'analyse'
end
The path /entry/analyse is matched two routes:
entry#show action in resources :entry
entry#analyse action in get 'entry/analyse', as: 'analyse'
Because the route matching is interpreted through config/routes.rb in order, and resources :entry is the first route the path is matched. At the result, entry#show action will handle the requests from /entry/analyse.
The solution is simple, just switch the order of resources :entry and get 'entry/analyse', as: 'analyse' in config/routes.rb. For example:
Rails.application.routes.draw do
get 'entry/analyse', as: 'analyse' # <- to here
resources :entry
devise_for :users
resources :users
root to: "entry#index"
get 'entry/index'
# from here ...
end
Move all of your resources to the end of your route configuration. I am not sure why its getting tripped up but it seems something within your routes is matching analyse to your entry resource and routes within rails are matched in order. All custom routes really should come first to prevent something like a generic route catching your expected action.
Best practices also state your root to: should be at the top of the configuration since it is your most popular route generally in an application.
I'm getting this error :
"The action 'create' could not be found for ObjectController"
I know it should be obvious but I'm missing something, that's my controller :
class ObjectController < ApplicationController
def index
end
def create
end
end
And that is my routes :
Rails.application.routes.draw do
get 'object/index'
get 'object/create'
match ':controller(/:action(/:id))', :via => :get
resources :objets
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'object#index'
You probably want to scrap those routes and try something simpler like
resources :objects, only: [:get, :create, :show]
Then use
$ rake routes
To make sure your routes are as the should be. You will want a POST route to /objects to create a new object etc..
Ok that one was dumb, actually I had two directories and I wasn't modifying the right one, sorry about that...
Your routes could be greatly improved:
#config/routes.rb
Rails.application.routes.draw do
root 'objects#index'
resources :objects
--
Next, the "standard" way to achieve what you're looking for is to use the new action; IE not the "create" action. If you wanted to use the create path name (instead of new), you'll be able to define it in the path_names argument:
#config/routes
resources :objects, path_names: { new: "create", create: "create" } #-> url.com/objects/create
To understand why you should be using new instead of create, you should look up resourceful routing, and how it pertains to object orientated programming.
Finally, your controller should be named in the plural:
#app/controllers/objects_controller.rb
class ObjectsController < ApplicationController
...
end
Whilst you can call it whatever you like, Rails defaults to plural controller names, singular model names.
I have a route in my Rails application that looks like this:
resources :products
get :specs, on: :member
end
This results in the route helper: specs_product_path instead of product_specs_path. How can I define the route so that the "specs action" is added to the end of the helper method instead of the beginning?
You can always declare a sub-resource which follows this convention:
resources :products do
resources :specs, only: [ :index ]
end
This will require creating another controller, though, with an index action.
You should also be able to override the name with the as: option:
resources :products do
get :specs, on: :member, as: :product_specs
end
Generally it's a good idea to adhere to convention as every exception can lead to confusion or conflict down the road.
When I go to /relationships/index it displays the show page even if it is not mentioned in the controller? Then when I try to just go to an index view without a show page created I get the following error: Unknown action: The action 'show' could not be found for relationships controller, even with no mention of it in the controller or a view file for the action.
routes.rb
Mymanual::Application.routes.draw do
resources :validation_rules
resources :validations
resources :product_types
resources :products
resources :connections
resources :relationships
root :to => 'products#index'
end
relationships_controller.rb
class RelationshipsController < ApplicationController
def index
end
def new
#relationship = Relationship.new
end
end
Then just HTML in a index.html.erb, show.html.erb and new.html.erb file.
It's because /relationship path is for relationships#index. If you go to /relationships/index, Rails router assumes you want to go to relationships#show path with params[:id] == 'index'.
Even though you want the index action, you don't enter index in the url. Otherwise, it thinks you're trying to find a Relationship with an id of index.
So just go to /relationships instead.
Go to a console and then run:
rake routes
It will give you all routes your app understand
In some place of this routes you will have something like:
relationships relationships#index /relationships
relationship relationships#show /relationships/:id
The issue that you have is that the app understand /relationships/index as relationships with id=index
The fix? use /relationships url
I am creating a website with a blog module. A blog post can either be a draft or published.
A published post can no longer be edited, and a draft cannot be viewed (only edit)
I currently have a resource defined as
resources :posts, :path => "blog" do
collection do
get 'drafts'
end
end
I can access the drafts list using blog/drafts, creating new ones posts using blog/new, and editing drafts through blog/:id/edit.
However, I'd like new drafts to be created using blog/drafts/new and edited using blog/drafts/:id
I need to define the new, create, edit and update routes to use this new scheme. The new and create routes seem quite simple. However I do not know how to handle the edit route in order to remove the action name part.
Also, while looking at the default routes definition, I found in actionpack-3.2.9/lib/action_dispatch/routing/mapper.rb the following :
member do
get :edit if parent_resource.actions.include?(:edit)
get :show if parent_resource.actions.include?(:show)
[...]
end
I do not understand how rails differentiates the :edit and the :show routes, and map the urls accordingly.
Thanks
You can use the following routes. Keep in mind that it requires different file hierarchy, rake routes should be your friend in this.
namespace :blog do
resources :drafts, :controller => :posts, only: [:new, :edit]
resources :posts, only: :show
end