route index for a resource - ruby-on-rails

What is the equivalent index path for resources :topics if I were to write it out manually?
When I run rake routes with the resources line in the routes.rb file it shows.
GET /topics(.:format) {:action=>"index", :controller=>"topics"}
I have tried a few things with no success, for example:
get 'topics/index' #=> 'topics#index'

as the route says:
get "/topics" => "topics#index", :as => :topics
you can now use topics_path or topics_url

Related

ArgumentError: Missing :controller key on routes definition

Rails.application.routes.draw do
resources :items
root 'items#index'
get 'items/index', to:'items/index'
get 'items/show', to:'items/show'
get 'items/new', to:'items/new'
get 'items/edit', to:'items/edit'
post '/items/create', to:'items/create'
post '/items/update', to:'items/update'
post '/items/destroy', to:'items/destroy'
get '/users/userindex', to: 'users/userindex'
get '/users/usershow', to: 'users/usershow'
get '/users/usernew', to: 'users/usernew'
end
ArgumentError: Missing :controller key
The notation used to map the routes to a controller's action is controller#action, not controller/action. Change your routes accordingly
Rails.application.routes.draw do
resources :items
root 'items#index'
get 'items/index', to:'items#index'
get 'items/show', to:'items#show'
get 'items/new', to:'items#new'
get 'items/edit', to:'items#edit'
post '/items/create', to:'items#create'
post '/items/update', to:'items#update'
post '/items/destroy', to:'items#destroy'
get '/users/userindex', to: 'users#userindex'
get '/users/usershow', to: 'users#usershow'
get '/users/usernew', to: 'users#usernew'
end
Moreover, you should take a look at Resourceful Routing. You have most of the routes declared wrongly. In other words, they wouldn't be needed when you already have them with resources

Routes on Rails application are failing

I'm taking an MOOC and the goal of this exercise is to add a new functionality to typo, where i can merge two articles together.
When I add the route to my new function merge to the routes.rb I'm losing the functionality to delete articles. I think something clashes here, but I have no idea what.
The original routes.rb:
%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end
This method in articles.rb creates the correct url for deleting
def delete_url
blog.url_for(:controller => "/admin/content", :action =>"destroy",:id => id)
end
correct url:
http://example.com/admin/content/destroy/7
If i follow this link i can successfully delete an article.
However, if I add the following before that to my routes.rb:
namespace "admin" do
resources :content do
post :merge, on: :member, as: :merge
end
end
The new merging functionality and forms are working fine, but the method delete_url now produces something like this:
http://example.com/admin/content/7
and if I follow a link created by this method i get:
Unknown action
The action 'show' could not be found for Admin::ContentController
Maybe I'm overwriting something? I can't figure out what's happening here and why this affects the delete action / route.
Thanks in advance!
EDIT: rake routes | grep content:
with the original routes.rb gives me:
admin_content /admin/content {:controller=>"admin/content", :action=>"index"}
/admin/content(/:action(/:id)) {:action=>nil, :id=>nil, :controller=>"admin/content"}
whereas my modified routes.rb produces
merge_admin_content POST /admin/content/:id/merge(.:format) {:action=>"merge", :controller=>"admin/content"}
admin_content_index GET /admin/content(.:format) {:action=>"index", :controller=>"admin/content"}
POST /admin/content(.:format) {:action=>"create", :controller=>"admin/content"}
new_admin_content GET /admin/content/new(.:format) {:action=>"new", :controller=>"admin/content"}
edit_admin_content GET /admin/content/:id/edit(.:format) {:action=>"edit", :controller=>"admin/content"}
admin_content GET /admin/content/:id(.:format) {:action=>"show", :controller=>"admin/content"}
PUT /admin/content/:id(.:format) {:action=>"update", :controller=>"admin/content"}
DELETE /admin/content/:id(.:format) {:action=>"destroy", :controller=>"admin/content"}
/admin/content {:controller=>"admin/content", :action=>"index"}
/admin/content(/:action(/:id)) {:action=>nil, :id=>nil, :controller=>"admin/content"}
Okay, thanks to #guitarman i worked through my routes code and found out I can add the following except:
namespace "admin" do
resources :content, except: [:index, :show, :update, :destroy, :edit, :new, :create] do
post :merge, on: :member, as: :merge
end
end
after that, the rake routes just shows the additional merge route I wanted and my destroy action works fine again.
Check rake routes command. I think there is a route /admin/content/:id which will be created by resources :content in the namespace "admin".
Your request to http://example.com/admin/content/7 will be catched be the defined route but I gess you have no show action in the controller.
Better:
namespace "admin" do
post "/content/:id/merge", to: "admin/content#merge", as: :merge
end
For more information about recources and the CRUD operations please see the rails routing guide.

Routing error in thumbs_up

I followed the steps indicated in this answer for setting up thumbs_up
https://stackoverflow.com/a/4963297/1643048
However I keep getting this error and have no idea how to fix it:
No route matches {:action=>"vote_up", :controller=>"posts", :id=>nil}
Can anyone help?
UPDATE:
exact error-
Routing Error
No route matches {:action=>"vote_up", :controller=>"posts", :id=>nil}
Try running rake routes for more information on available routes.
config/routes.rb:
Projectmadrone::Application.routes.draw do
devise_for :admins
#devise_for :users
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :posts do
member do
post :vote_up
end
end
root :to => 'posts#index'
end
my view: app/views/posts/index.html.erb
<li><%= link_to('vote for this post!', vote_up_post_path(#post), :method => :post) %></li>
Your #post-variable seems to be nil. Propably because you are using it in the index-view instead of the show-view. So you should try to put the link_to-code into the app/views/posts/show.html.erb.

Rails routing url name helpers

What basic settings are required to make sure routing url name helpers work?
For instance in my route I have the following:
Blog::Application.routes.draw do
resources :news, :as => :news_items, :controller => :news_items, :only => [:show, :index]
scope :module => "refinery" do
scope(:path => 'refinery', :as => 'admin', :module => 'Admin') do
resources :news, :except => :show, :as => :news_items, :controller => :news_items
end
end
end
but the following doesn't seem to work:
new_refinery_news_url
I keep on getting the error
undefined local variable or method `new_refinery_news_url'
So I'm pretty sure something is missing in the way I have configured my application, who's main routing is in the RefineryCMS gem which was added in the Gemfile.
Any thoughts?
Had to use main_app.new_refinery_news_url instead.
The helper name will be new_admin_news_item_url.
It's simple to find all routes and their helper methods. Just run rake routes and you will see:
news_items GET /news(.:format) {:action=>"index", :controller=>"news_items"}
news_item GET /news/:id(.:format) {:action=>"show", :controller=>"news_items"}
admin_news_items GET /refinery/news(.:format) {:action=>"index", :controller=>"refinery/Admin/news_items"}
POST /refinery/news(.:format) {:action=>"create", :controller=>"refinery/Admin/news_items"}
new_admin_news_item GET /refinery/news/new(.:format) {:action=>"new", :controller=>"refinery/Admin/news_items"}
edit_admin_news_item GET /refinery/news/:id/edit(.:format) {:action=>"edit", :controller=>"refinery/Admin/news_items"}
admin_news_item PUT /refinery/news/:id(.:format) {:action=>"update", :controller=>"refinery/Admin/news_items"}
DELETE /refinery/news/:id(.:format) {:action=>"destroy", :controller=>"refinery/Admin/news_items"}
With mountable engines you always need to specify "main_app." (or for Refinery routes "refinery.") prefix because engines are isolated from the application.
A solution, if you're using routes outside of refinery, is to prefix the named_path with the Rails object that contains the methods for named routes
Rails.application.routes.url_helpers.new_admin_news_item_path

Can't access path specified by rake routes command

I can't seem to correctly understand the routing in rails 3.1.
(keeping in mind I'm working on a project that depends on the refinery cms gem)
In my routes I have the following:
Blog::Application.routes.draw do
resources :news, :as => :news_items, :controller => :news_items, :only => [:show, :index]
scope :module => "refinery" do
scope(:path => 'refinery', :as => 'refinery_admin', :module => 'admin') do
resources :news, :as => :news_items, :controller => :news_items
#resources :news, :except => :show, :as => :news_items, :controller => :news_items
end
end
end
The output of the rake routes command is:
news_items GET /news(.:format) {:action=>"index", :controller=>"news_items"}
news_item GET /news/:id(.:format) {:action=>"show", :controller=>"news_items"}
refinery_admin_news_items GET /refinery/news(.:format) {:action=>"index", :controller=>"refinery/admin/news_items"}
POST /refinery/news(.:format) {:action=>"create", :controller=>"refinery/admin/news_items"}
new_refinery_admin_news_item GET /refinery/news/new(.:format) {:action=>"new", :controller=>"refinery/admin/news_items"}
edit_refinery_admin_news_item GET /refinery/news/:id/edit(.:format) {:action=>"edit", :controller=>"refinery/admin/news_items"}
refinery_admin_news_item GET /refinery/news/:id(.:format) {:action=>"show", :controller=>"refinery/admin/news_items"}
PUT /refinery/news/:id(.:format) {:action=>"update", :controller=>"refinery/admin/news_items"}
DELETE /refinery/news/:id(.:format) {:action=>"destroy", :controller=>"refinery/admin/news_items"}
The following code in my application leads to an error:
<%= form_for [:refinery, #news_item] do |f| %>
<% end %>
Telling me that the following path:
undefined method `refinery_news_items_path' for #<#<Class:0x0000010663c480>:0x00000106623980>
doesn't exist.
Any path from the rake routes command would just not work. I'm pretty confident that's it's a setting issue. I'm actually kind of writing a plugin to the RefineryCMS gem without actually using the plugin generator, I'm just building it as if it was a normal web app.
My project is hosted here for those who care to take a peak. http://github.com/mabounassif/blog
Anyone knows what might be the problem?
You're scoping your "refinery" scope as "refinery_admin" so when you did your rake routes, you got refinery_admin_news_items
if you take out that :as => 'refinery_admin' clause, your routes will return back to "normal"
It seems that the problem was with the way the Refinery CMS gem works. Apparently I shouldn't use the url helper right away, I should use the following instead:
main_app.new_refinery_admin_news_item_path

Resources