What is wrong with my routes? - ruby-on-rails

root :to => "index#home"
#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get
match "/tattoo" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get
members section and its nested images
resources :members, :except => [:new, :create] do
resources :tattoos
end
Thats whats in my routes.rb file. They produce:
root /(.:format) {:controller=>"index", :action=>"home"}
submit GET /submit(.:format) {:controller=>"index", :action=>"new"}
tattoo POST /tattoo(.:format) {:controller=>"index", :action=>"create"}
GET /tattoo/:id(.:format) {:controller=>"index", :action=>"show"}
tagged GET /tagged(.:format) {:controller=>"index", :action=>"tagged"}
DELETE /tattoo/:id(.:format) {:controller=>"index", :action=>"destroy"}
tattoos GET /tattoos(.:format) {:controller=>"index", :action=>"index"}
members GET /members(.:format) {:action=>"index", :controller=>"members"}
edit_member GET /members/:id/edit(.:format) {:action=>"edit", :controller=>"members"}
member GET /members/:id(.:format) {:action=>"show", :controller=>"members"}
PUT /members/:id(.:format) {:action=>"update", :controller=>"members"}
DELETE /members/:id(.:format) {:action=>"destroy", :controller=>"members"}
But i have a problem. For some reason, when I try to go to mysite.com/submit
I used to get this error
No route matches {:controller=>"images"}
on
<%= form_for #tattoo, :html =>{:multipart => true} do |f| %>
but that has magically changed to:
undefined method `images_path'
on the same line.
when my controller has this:
indexcontroller
def new
#tattoo = Image.new
end
def create
#tattoo = Image.new(params[:image])
if #tattoo.save
flash[:success] = "Tattoo sent in for approval!"
redirect_to(images_path)
else
render :action => "new"
end
end
And then this link_to:
<%= link_to "Manage tattoos", member_tattoos_path() %>
give me this error:
No route matches {:controller=>"tattoos"}
I thought I was beginning to understand routes and had a decent grasp but I dont get whats going on!

You need to pass in a member object to edit_member_path.
<%= link_to "Edit profile", edit_member_path(#member) %>

edit_member_path should know the id of the member you want to edit. Please try
<%= link_to "Edit profile", edit_member_path(#member) %>
For No route matches {:controller=>"images"}; since the action image is not defined in your route, please try to stop and restart the server and check if there is any plugin like Paperclip in place.

Related

I am working on An Ruby on Rails app

I am working an an Rails app. The problem I am have is when I cycle between my about and contact pages. I always get the error
No route matches [GET] "/pages/pages/about"
or
No route matches [GET] "/pages/pages/contact"
I'm trying to change the routes my nav bar partial the tag href to "/about" but the same error occur. it ask me to use the command rake routes and it shows
$ rake routes
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
restaurant GET /restaurants/:id(.:format) restaurants#show
PUT /restaurants/:id(.:format) restaurants#update
DELETE /restaurants/:id(.:format) restaurants#destroy
pages_about GET /pages/about(.:format) pages#about
root / restaurants#index
pages_contact GET /pages/contact(.:format) pages#contact"
can some one please help me!!
Your about and contact are under the url /pages/about and /pages/contact but the URL you are accessing is /pages/pages/about which doesn't exist.
You can see in the rake routes, the possible urls in the system.
you need to have the link in your web app as
<%= link_to 'About', pages_about_path %>
<%= link_to 'Contact', pages_contact_path %>
in your routes you can do something like
#config/routes.rb
ClientCruiser::Application.routes.draw do
....
match "contact" => "pages#contact", :as => :contact, via: :all
match "about" => "pages#about", :as => :about, via: :all
....
root :to => 'pages#index'
end
The output of this
contact /contact(.:format) pages#contact
about /about(.:format) pages#about
root GET / pages#index
the way you would make the call would be
<%= link_to about_path, "About", class: '' %>
<%= link_to contact_path, "Contact", class: '' %>

ruby on rails button_to not activating the delete method

Hi im following the agile web development ebook and i cant seem to activate the logout action
here are the revelant parts (TAB key not working could not format to code)
rake routes
logout DELETE /logout(.:format) sessions#destroy
from the route file
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
my controller
def destroy
session[:user_id] = user.id
redirect_to store_url , notice: "Logged out"
end
and my view (relevant part)
<%= button_to 'Logout', logout_path, method: :delete %>
the error message is
No route matches [GET] "/logout"
i know it should use delete method but nothing i do seems to help
You may need to add a match in your routes. Sorry that I don't have the book with me to refer to.
Put this above your controller :sessions ...
match 'logout' => 'sessions#destroy', :as => :logout
If you didn't put the above line, your logout path should be sessions_logout_path, not logout_path.
Reference:
http://guides.rubyonrails.org/routing.html#naming-routes
match '/logout' => 'sessions#destroy', :via => :delete
or
controller :sessions do
member do
delete :destroy, :as => :logout
end
end

'show' route matched instead 'new'

rails 3.1
rake routes for admin/sections_contoller
trigger_admin_section GET /admin/sections/:id/trigger(.:format) {:action=>"trigger", :controller=>"admin/sections"}
admin_sections GET /admin/sections(.:format) {:action=>"index", :controller=>"admin/sections"}
POST /admin/sections(.:format) {:action=>"create", :controller=>"admin/sections"}
new_admin_section GET /admin/sections/new(.:format) {:action=>"new", :controller=>"admin/sections"}
edit_admin_section GET /admin/sections/:id/edit(.:format) {:action=>"edit", :controller=>"admin/sections"}
admin_section GET /admin/sections/:id(.:format) {:action=>"show", :controller=>"admin/sections"}
PUT /admin/sections/:id(.:format) {:action=>"update", :controller=>"admin/sections"}
DELETE /admin/sections/:id(.:format) {:action=>"destroy", :controller=>"admin/sections"}
routes.rb
namespace :admin do
resources :sections do
resources :items
resources :parameters
get :trigger, :on => :member
end
...
end
view
<%= link_to "Add a section", new_admin_section_path, :class=>'add-btn' %>
generated link
http://localhost:3000/admin/sections/new
result
No route matches {:action=>"show", :controller=>"admin/sections",
:id=>#<Section id: nil, ..., meta_description: nil}
strange bug or my mistake. other controllers has similar routes and all works fine.
for ex.:
<%= link_to 'Add a group', new_admin_group_path, :class=>'add-btn' %>
works GREAT!
please, help or i'll kill myself someday
upd1 same problem on heroku with this app.
upd2 join github issue: https://github.com/rails/rails/issues/4704
i found the answer.
once i put this string in view 'admin/section/_form'
<%= link_to 'delete', admin_section_path(#section), :method => :delete, :confirm => "Sure?" %>
i used this form for creating and editing. so combination of new object and deleting link for it caused the bug.
i used debugger for analysis.
Look at your "create" method in sections_controller.
I guess your section is correctly created but it redirect to "show" action. And "show" view may not exist.
Have you checked your database ? Is the section saved ?

custom route no routes match

During cucumber tests, I get the following error:
No route matches "/companies/29/update_owner"
I am not sure what I am doing wrong, but im sure its something stupid.
I have the following routes:
company_update_owner POST /companies/:company_id/update_owner(.:format) {:controller=>"companies", :action=>"update_owner"}
company_set_owner /companies/:company_id/set_owner(.:format) {:controller=>"companies", :action=>"set_owner"}
companies GET /companies(.:format) {:action=>"index", :controller=>"companies"}
POST /companies(.:format) {:action=>"create", :controller=>"companies"}
new_company GET /companies/new(.:format) {:action=>"new", :controller=>"companies"}
edit_company GET /companies/:id/edit(.:format) {:action=>"edit", :controller=>"companies"}
company GET /companies/:id(.:format) {:action=>"show", :controller=>"companies"}
PUT /companies/:id(.:format) {:action=>"update", :controller=>"companies"}
DELETE /companies/:id(.:format) {:action=>"destroy", :controller=>"companies"}
I have tried:
company_update_owner_path(:company_id => #company.id)
and
company_update_owner_path(#company)
update:
= form_for #company, :url => company_update_owner_path(:company_id => #company.id), :method => :put do |f|
Any help would be greatly appreciated.
Here is the code in routes.rb:
resources :companies do
match '/update_owner' => 'companies#update_owner', :as => :update_owner, :via => :post
match '/set_owner' => 'companies#set_owner', :as => :set_owner
end
you need to remove the ":method=>:put"
Your route is declared as a POST but you're doing a PUT in the form. Fix either the route or the form and you'll nail it.
Would leave this as a comment, but the fromatting gets jacked.
If you're not aware, you can also define those routes slightly differently, too:
resources :companies do
member do
post "update_owner"
get "set_owner"
end
end

Trouble on setting the ':controller' parameter for a 'link_to' statement

I am using Ruby on Rails 3.0.9 and I would like to know why I get the error described below and how can I solve that.
In my /views/articles/categories/_content.html.erb file I have:
...
<%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %>
...
If I set the content[:article_controller] to (both setting true and false for the :only_path option)
1. content[:article_controller] = 'articles'
2. content[:article_controller] = '/articles'
3. content[:article_controller] = '/articles/'
4. content[:article_controller] = '/'
4. content[:article_controller] = ''
I get respectively the following errors (note :controller values):
1. `ActionView::Template::Error (No route matches {:controller=>"articles/categories/articles", :action=>"new"})`
2. `ActionView::Template::Error (No route matches {:controller=>"articles//articles", :action=>"new"})`
3. `ActionView::Template::Error (No route matches {:controller=>"articles/", :action=>"new"})`
4. `ActionView::Template::Error (No route matches {:controller=>"articles//", :action=>"new"})`
4. `ActionView::Template::Error (No route matches {:controller=>"articles/categories/", :action=>"new"})`
Is it a Ruby on Rails bug or is it my fault? What is the problem and how can I solve that making the link_to properly work?
However I can solve that problem by using:
<%= link_to("New article", {:controller => '../', :action => 'new'}) %>
But why it works with '.../' but not in other ways?
I noticed that some time the controller path for which I try to set the content[:article_contr8oller] seems to relying on the "base" current controller path that is handling the view file (the controller file is app/controllers/articles/categories/concerns_controller.rb - read below for more information)... why it happens?
It also happens using url_for:
url_for(:controller => 'articles', :action => 'new')
Running the rake routes command I get the following:
articles_categories GET /articles/categories(.:format) {:action=>"index", :controller=>"articles/categories"}
POST /articles/categories(.:format) {:action=>"create", :controller=>"articles/categories"}
new_articles_category GET /articles/categories/new(.:format) {:action=>"new", :controller=>"articles/categories"}
edit_articles_category GET /articles/categories/:id/edit(.:format) {:action=>"edit", :controller=>"articles/categories"}
articles_category GET /articles/categories/:id(.:format) {:action=>"show", :controller=>"articles/categories"}
PUT /articles/categories/:id(.:format) {:action=>"update", :controller=>"articles/categories"}
DELETE /articles/categories/:id(.:format) {:action=>"destroy", :controller=>"articles/categories"}
articles GET /articles(.:format) {:action=>"index", :controller=>"articles"}
POST /articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_article GET /articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
P.S.: If you need more information, let me know and I will update the question as well.
UPDATE I
In my route file I have:
namespace :articles do articles :categories end
scope :path => 'articles/categories/:id', :controller => 'articles/categories/concerns' do
...
end
resources :articles
UPDATE II
In my view /views/articles/categories/_content.html.erb files I have:
<div class="links">
<%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %>
</div>
In my Articles::Categories::ConcernsController (that is, in the app/controllers/articles/categories/concerns_controller.rb file) I have:
def show
#articles_category = Articles::Category.find(params[:id])
respond_to do |format|
format.html {
render :partial => '/views/articles/categories/_content.html.erb',
:locals => {
:content => {
:article_controller => '/articles'
}
}
format.js {
...
end
end
end
Did you try using symbols? I think they are more "direct".
<%= link_to("New article", {:controller => content[:article_controller].to_sym, :action => :new}) %>
Did you try using a relativ path?
<%= link_to("New article", {:controller => "../#{content[:article_controller]}", :action => 'new'}) %>
Why aren't you using link_to 'New article', new_article_path? Why use the old, tired url_for ... when you can use the named path/url helper (new_article_url).

Resources