ToriBean
ToriBean

Reputation: 27

Rails 3 - No Route Matches Error

This is my first question to stackoverflow, and I feel a bit daft having not been able to fix the bug myself, but here goes.

I'm attempting to connect a link on a pages index to an edit action and I get the following error:

No route matches {:action=>"edit", :controller=>"pages"}

So obviously I first checked the controller - there's definitely an edit action in there!

Here's my relevant rake routes output:

      pages GET    /pages(.:format)          pages#index
            POST   /pages(.:format)          pages#create
   new_page GET    /pages/new(.:format)      pages#new
  edit_page GET    /pages/:id/edit(.:format) pages#edit
       page GET    /pages/:id(.:format)      pages#show
            PUT    /pages/:id(.:format)      pages#update
            DELETE /pages/:id(.:format)      pages#destroy

And my config/routes.rb:

  Portfolio::Application.routes.draw do
  resources :pages
  resources :sessions, only: [:new, :create, :destroy]
  resources :users

  root to: 'pages#home'

  match '/signup',   to: 'users#new'
  match '/signin',   to: 'sessions#new'
  match '/signout',  to: 'sessions#destroy', via: :delete
  match '/admin',    to: 'admin#index'
  match '/new',      to: 'pages#new'
  match '/edit',     to: 'pages#edit'

  match '*path', :controller => 'redirect', :action => 'index'

And the controller action, just in case.

  def edit
    @page = Page.find(params[:id])
  end

I thought I'd also give the view, where the path is called:

 <% Page.where("parent_id IS NULL").each do |page| %>
   <li>
     <%= link_to page.title, page %>
     <%= link_to "Edit", page, edit_page_path  %>
     <%= link_to "Delete", page, method: :delete,
                                 data: { confirm: "You sure?" } %>
   </li>
 <% end %>

I do have an awful habit of not seeing my typos, so if I have made a silly one it would explain why I can't fix this one.

Any help would be appreciated.

Thanks!

Upvotes: 0

Views: 1666

Answers (2)

Erez Rabih
Erez Rabih

Reputation: 15788

Try:

 <%= link_to "Edit", edit_page_path(page)  %>

If you look closer on your routes output:

edit_page GET    /pages/:id/edit(.:format) pages#edit

The route edit_page requires an :id to be passed to.

Upvotes: 1

Kulbir Saini
Kulbir Saini

Reputation: 3915

The edit link should be generated like this

<%= link_to "Edit", edit_page_path(page) %>

As Erez pointed out, :id is required which is retrieved from page.

Check link_to signatures for possible ways to pass the arguments. First/second argument is for url or url_options while third argument is for html_options.

Upvotes: 0

Related Questions