Problems with routing and path - ruby-on-rails

I am new to rails and I have a weird problem i don't understand...
I have created a basic application with only one controller. this controller is name routes (for testing purpose...) and it contains index, new and edit actions.
I have added a resource in the routes.rb file: map.resources :routes
The problem I have is when i try to make a link to an action like link_to edit_route_path(some id) I get the error undefined local variable or method `path' for #ActionController::Routing::RouteSet:0x101f4d088>
When I use routes_path directly, it works fine.
Thanks for your help!
output of rake routes:
routes GET /routes(.:format) {:controller=>"routes", :action=>"index"}
POST /routes(.:format) {:controller=>"routes", :action=>"create"}
new_route GET /routes/new(.:format) {:controller=>"routes", :action=>"new"}
edit_route GET /routes/:id/edit(.:format) {:controller=>"routes", :action=>"edit"}
route GET /routes/:id(.:format) {:controller=>"routes", :action=>"show"}
PUT /routes/:id(.:format) {:controller=>"routes", :action=>"update"}
DELETE /routes/:id(.:format) {:controller=>"routes", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)
this is the error I have:
undefined local variable or method `path' for #ActionController::Routing::RouteSet:0x101f4d128>
and the stack trace:
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:386:in generate'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:208:inrewrite_path'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:187:in rewrite_url'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:165:inrewrite'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:625:in url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/url_helper.rb:85:insend'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/url_helper.rb:85:in url_for'
(eval):17:inedit_blog_path'
/Users/guillaume/Projets/rails/testroutes/app/views/blogs/edit.html.erb:4:in `_run_erb_app47views47blogs47edit46html46erb'
ruby version is 1.8.7
gem version is 1.3.7
rails version is 2.3.8
I tried the basic posts scaffold from the rails getting started user guide and i have the same error when I am in the new page or edit page...
ActionController::Routing::Routes.draw do |map|
map.resources :routes
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
The weird thing is that everything was working fine last week and I don't know what i have changed...
Thank you very much!!!

Go to terimnal and type
rake routes
It will show you all possible routes define in your routes.rb file.
Then check edit_route_path is there or not

Related

Using URL helpers from the console in Ruby on Rails 2.3.11

I'm trying to use URL helpers in a 2.3.11 Ruby on Rails application, but I'm facing many problems with whatever the solution I try...
I've read a lot of "solutions" like including include ActionController::UrlWriter before trying to get a path, but I get the same error every time:
>> app.users_path
NameError: undefined method `new_suggestion_path' for class `ActionController::Integration::Session'
I can't find the problem with this. I have a "suggestions" controller and a "new" action on it... Is there any other way to fix this and get a URL from console and/or Rake?
You need to have resource routes defined for the named routes you are trying to access.
To make suggestion paths available in Rails 2.3, add a resource route config/routes.rb:
ActionController::Routing::Routes.draw do |map|
map.resources :suggestions
end
That will define the following routes:
suggestions GET /suggestions(.:format) {:controller=>"suggestions", :action=>"index"}
POST /suggestions(.:format) {:controller=>"suggestions", :action=>"create"}
new_suggestion GET /suggestions/new(.:format) {:controller=>"suggestions", :action=>"new"}
edit_suggestion GET /suggestions/:id/edit(.:format) {:controller=>"suggestions", :action=>"edit"}
suggestion GET /suggestions/:id(.:format) {:controller=>"suggestions", :action=>"show"}
PUT /suggestions/:id(.:format) {:controller=>"suggestions", :action=>"update"}
DELETE /suggestions/:id(.:format) {:controller=>"suggestions", :action=>"destroy"}
Now you can use those routes in the console:
>> app.new_suggestion_path
=> "/suggestions/new"
>>

Routes stopped working when upgrading Rails 3.0 to 3.1

I am using Ruby 1.8.7 with Rails 3.1. My application worked fine in Rails 3.0 but when I bumped it up to Rails 3.1.4, all my url helpers broke!
After Googling like a mad man the past 2 days, I have given up and the time has come to seek help. I don't believe the problem is with my routes.rb file but something more on the view/helper side.
I have the following in my routes.rb:
resources :sessions
In my homepage view I have the following link_to, which errors out:
<%= link_to "Login", new_session_path %>
When I do rake routes, I get the following output, so the path exists:
sessions GET /sessions(.:format) {:controller=>"sessions", :action=>"index"}
POST /sessions(.:format) {:controller=>"sessions", :action=>"create"}
new_session GET /sessions/new(.:format) {:controller=>"sessions", :action=>"new"}
edit_session GET /sessions/:id/edit(.:format) {:controller=>"sessions", :action=>"edit"}
session GET /sessions/:id(.:format) {:controller=>"sessions", :action=>"show"}
PUT /sessions/:id(.:format) {:controller=>"sessions", :action=>"update"}
DELETE /sessions/:id(.:format) {:controller=>"sessions", :action=>"destroy"}
When I go to /sessions/new in my browser, the page loads so again, the route exists, but it errors out on a _path based url:
<%= form_tag sessions_path, :method => :post do -%>
The error I get is as follows:
ActionView::Template::Error (undefined local variable or method `sessions_path' for #<#<Class:0x109cb8900>:0x109cab840>):
It has to be something with the url_for helper as the routes do exist. What else should I look for?
UPDATE 1:
I added the following inside application_helper.rb:
include Rails.application.routes.url_helpers
Now when I get the following error:
In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
Isn't that is what I just did?
Update 2:
The following worked as MrYoshiji suggested:
Rails.application.routes.url_helpers.sessions_path
Update 3:
I got sessions_path working again by removing some old Rails 2 plugins in vendor directory.
Removed some Rails 2 plugins in vendor/plugins did the trick.

Rails 3 restful route problem

The following link works in my app:
<%= link_to "invitation", :controller => :invitations, :action => :index %>
To follow restful conventions i changed the link to:
<%= link_to "invitation", index_invitation_path %>
The error that i get is:
undefined local variable or method `index_invitation_path'
Rake routes yields:
invitations GET /invitations(.:format) {:controller=>"invitations", :action=>"index"}
The page name is index.html.erb. The model is invitation.rb. The controller is invitation_controller.rb. Routes has resources :invitations. What am i missing?
Thanks!
Assuming you have the routing correct:
resources :invitations
Then the correct helper for the index action (with the url /invitations.html) is
invitations_path
You can see more information by running rake routes. It will display text like the following:
lists GET /lists(.:format)
{:action=>"index", :controller=>"lists"}
POST /lists(.:format)
{:action=>"create", :controller=>"lists"}
new_list GET /lists/new(.:format)
{:action=>"new", :controller=>"lists"}
edit_list GET /lists/:id/edit(.:format)
{:action=>"edit", :controller=>"lists"}
list GET /lists/:id(.:format)
{:action=>"show", :controller=>"lists"}
PUT /lists/:id(.:format)
{:action=>"update", :controller=>"lists"}
DELETE /lists/:id(.:format)
{:action=>"destroy", :controller=>"lists"}
root /(.:format)
{:controller=>"lists", :action=>"index"}
The above was from a route of my own (for a model called List). The route helper method is shown immediately before the HTTP method. You have to remember to append the _path to each helper method. For example the helper methods I could use are:
list_path(list)
edit_list_path(list)
new_list_path
lists_path
You'll need a route in your routes.rb file that defines a mapping to the invitations controller and the index action.
Typically this is created with a resources call
resources :invitations
Which creates several default routes, which you can see by running rake routes.
For single resources, you can also define it using a match call
match "invitations/:id" => "invitations#index", :as => index_invitation
The rails site has a great resource on routing that provides all the details: Routing from the Outside In
Update: Based on your updated question, your route includes an invitaions (notice the trailing 's') route - nothing with index or invitation. The index_ prefix is generated by the resources call when it creates the default routes for :invitations.
It looks like you've defined a custom get mapping for an invitation. While this may technically work, if you're aim is to support restful routes, use the resources method. And have a read of the Routing guide from rails it's very easy to follow and quite detailed.
type rake routes in your console and look at listing of available routes. Seems to be there is no such route index_invitation_path? maybe it named differently
I think you need "invitations_controller.rb" to contain InvitationsController. Plural.

rails foobar_path(3) returnes strange path: "/foobar.3/" instead of "/foobar/3/

Hi i have this starnge behavoir...
<%= link_to image_tag("image.png"), brain_path(1), :method => "put" %>
produces:
<img alt="Research_4" src="/images/image.png" />
a href="/foobar.1" this is the strange part :( any ideas whqt is causing this?
rake routes gives the following:
new_brain GET /brain/new(.:format) {:controller=>"brains", :action=>"new"}
edit_brain GET /brain/edit(.:format) {:controller=>"brains", :action=>"edit"}
brain GET /brain(.:format) {:controller=>"brains", :action=>"show"}
PUT /brain(.:format) {:controller=>"brains", :action=>"update"}
DELETE /brain(.:format) {:controller=>"brains", :action=>"destroy"}
POST /brain(.:format) {:controller=>"brains", :action=>"create"}
How do you route your foobar? (singular or plural? resource or resources?)
Are you sure that you're using foobar_path(1), not foobars_path(1) (singular form)
in real life foobars_path(1) will return /foobars.1 and foobar_path(1) - /foobar/1
as I see you have to use brain_path(1) not brain_path(1)
UPD
Change your route.rb
map.resources :brain
it will be better if you'll rename your controller into pluralize brains - it is more conventional when you are using resources

Ruby on rails model and controllers inside of different namespaces

OK. This is insane.
I'm new to RoR and I really want to get into it as everything about it that I have seen so far makes it more appealing to the type of work that I do.
However, I can't seem to accomplish a very simple thing with RoR.
I want these controlers:
/admin/blog/entries (index/show/edit/delete)
/admin/blog/categories (index/show/edit/delete)
/admin/blog/comments (index/show/edit/delete)
... and so on
And these models:
Blog::Entry (table: blog_entries)
Blog::Category (table: blog_categories)
Blog::Comments (table: blog_comments)
... and so on
Now, I have already gone though quite a bit of misery to make this work. My first attempt was with generating scaffolding (I'm using 2.2.2). I generated my scaffolding, but had to move my model, then fix the references to the model in my controller (see Ruby on Rails model inside namespace can't be found in controller).
That is already a big of a pain, but hey, I got it to work. Now though form_for won't work and I cannot figure out how to use the url helpers (I have no idea what these are called... they are the automatically generated methods that return URLs to controllers associated with a model). I cannot figure out what their name is. My model is Blog::Entries. I have tried to mess with the route.rb's map's resource method, but no luck. When I attempt to use form_for with my model, I get this error
undefined method `blog_entries_path' for #<ActionView::Base:0xb6848080>
Now. This is really quite frustrating. I am not going to completely destroy my code's organization in order to use this framework, and if I cannot figure out how to accomplish this simple task (I have been researching this for at least 5 hours) then I simply cannot continue.
Are there any ideas on how to accomplish this?
Thanks
EDIT
Here are my routes:
admin_blog_entries GET /admin_blog_entries {:controller=>"admin_blog_entries", :action=>"index"}
formatted_admin_blog_entries GET /admin_blog_entries.:format {:controller=>"admin_blog_entries", :action=>"index"}
POST /admin_blog_entries {:controller=>"admin_blog_entries", :action=>"create"}
POST /admin_blog_entries.:format {:controller=>"admin_blog_entries", :action=>"create"}
new_admin_blog_entry GET /admin_blog_entries/new {:controller=>"admin_blog_entries", :action=>"new"}
formatted_new_admin_blog_entry GET /admin_blog_entries/new.:format {:controller=>"admin_blog_entries", :action=>"new"}
edit_admin_blog_entry GET /admin_blog_entries/:id/edit {:controller=>"admin_blog_entries", :action=>"edit"}
formatted_edit_admin_blog_entry GET /admin_blog_entries/:id/edit.:format {:controller=>"admin_blog_entries", :action=>"edit"}
admin_blog_entry GET /admin_blog_entries/:id {:controller=>"admin_blog_entries", :action=>"show"}
formatted_admin_blog_entry GET /admin_blog_entries/:id.:format {:controller=>"admin_blog_entries", :action=>"show"}
PUT /admin_blog_entries/:id {:controller=>"admin_blog_entries", :action=>"update"}
PUT /admin_blog_entries/:id.:format {:controller=>"admin_blog_entries", :action=>"update"}
DELETE /admin_blog_entries/:id {:controller=>"admin_blog_entries", :action=>"destroy"}
DELETE /admin_blog_entries/:id.:format {:controller=>"admin_blog_entries", :action=>"destroy"}
home / {:action=>"index", :controller=>"index"}
/:controller/:action/:id
/:controller/:action/:id.:format
That dosn't look right. Here is my routes.rb (comments removed):
ActionController::Routing::Routes.draw do |map|
map.resources :admin_blog_entries
map.home '', :controller => 'index'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
have you tried looking at the routes list that "rake routes" gives you? if your routes.rb is correct, it should show you the correct name for the blog entries route.
also, maybe this can help: http://www.coreywoodcox.com/2008/08/18/rails-namespaces-subdomains/.
edit:
well, then the correct way to call the route is admin_blog_entries_path instead of blog_entries_path.
Your routes.rb should look like this:
map.namespace :admin do |admin|
admin.namespace :blog do |blog|
blog.resources :entries
blog.resources :categories
...
end
end
but I'm not sure how to handle this '/blog/' part in your url (I didn't use any namespace in my models yet). But with these routes you will be able to use:
admin_blog_categories_path => '/admin/blog/categiries'
admin_blog_category_path(#some_category) => '/admin/blog/categories/1'
and so on.
OK, here my rather hacky way of doing it, which I don't like but does work.
In my case I have the models Blog::Article, Blog::Comment, they are nested in the routes. One caveat if using this approach is in the Blog::CommentsController when loading the article you can expect params[:article_id] or params[:blog_article_id]. By no means nice, but like I said. It does work :/
blog.resources :articles do |article|
article.resources :comments
end
blog.resources :blog_articles, :controller => 'articles' do |blog_article|
blog_article.resources :blog_comments, :controller => 'comments'
end

Resources