Question

I have a routing error that I simply cannot figure out! Its doing my head in, if anyone can suggest a solution that would be hugely appreciated.

I get the error: ROUTING ERROR No route matches {:action=>"create_from_template", :controller=>"projects"}

from the following button:

<p><%= link_to "Create from template", create_from_template_project_path %></p>

In routes.rb I have:

  resources :projects do
    member do
      get 'create_from_template'
    end
  end

In class ProjectsController I have:

  def create_from_template
    #@project = Project.find(params[:template_id])
    #@project.clone
    redirect_to projects_path
  end

It also shows up when I do "rake routes"

create_from_template_project GET    /projects/:id/create_from_template(.:format) {:action=>"create_from_template", :controller=>"projects"}

Anyone have any idea why it isnt working?

EDIT: Actually maybe I have misunderstood the "member" nested resource routing rules. I wasnt passing in a project. I have changed the button from

to

   <p><%= link_to "Create from template", create_from_template_project_path(template_project) %></p>

and now it works. Thanks everyone that helped.

Was it helpful?

Solution

If you dont need to pass project for creating template action,change button as: `

 <%= link_to "Create from template", create_from_template_projects_path %> 
In routes, instead of
 member do 
you should use
 collection do 

If you use member do in routes.rb then you should pass project in the link_to as Prasvin has mentioned

OTHER TIPS

try sending the project in the path alongside link_to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top