Routing issue. No routes found, but I know these exist

Noob here.

Trying to figure out how to display a method in my controller into my index page. Here is what I have thus far.

Controller -

 class SammichesController < ApplicationController
  def index
    @sammiches = Sammich.all
end
  def create
    @sammich = Sammich.find_by_name(params[:sammich][:name])
  end
  def random
    @sammichy = Sammich.rand(params[:sammich][:name])
  end
end

Routes-

Sammiches::Application.routes.draw do
resources :sammiches do
  get "random"
end
root :to => 'sammiches#index'

Index-

<h1>All my Sammiches</h1>
<%= form_for Sammich.new do |f| %>
<%= f.label :sammich %>
<%= f.text_field :name %>
<%= f.submit 'Find Sammich', :id => 'sammich_submit' %>
<% end %>
<%= link_to "Random sandwich", sammich_random_path %>

routes-

sammich_random GET    /sammiches/:sammich_id/random(.:format) sammiches#random
     sammiches GET    /sammiches(.:format)                    sammiches#index
               POST   /sammiches(.:format)                    sammiches#create
   new_sammich GET    /sammiches/new(.:format)                sammiches#new
  edit_sammich GET    /sammiches/:id/edit(.:format)           sammiches#edit
       sammich GET    /sammiches/:id(.:format)                sammiches#show
               PUT    /sammiches/:id(.:format)                sammiches#update
               DELETE /sammiches/:id(.:format)                sammiches#destroy
          root        /                                       sammiches#index

Error-

localhost:3000 - Routing Error

No route matches {:action=>"random", :controller=>"sammiches"}
Try running rake routes for more information on available routes.

Any assistance would be much appreciated.

Thanks!

Answers


If you look at your route it has :sammic_id in there as well:

sammich_random GET    /sammiches/:sammich_id/random(.:format) sammiches#random

Which means you need to pass an id to your URL helper sammich_random_path which you haven't.

Update your routes to this:

resources :sammiches do
  collection do
    get "random"
  end
end

After adding that your route would be just /sammiches/random


Need Your Help

Shell in the C Programming language

c shell operating-system

I want to write a shell in the Linux operating system with the C language.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.