0

ノブはこちら。

コントローラーのメソッドをインデックス ページに表示する方法を見つけようとしています。これが私がこれまでに持っているものです。

コントローラ -

 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

ルート-

Sammiches::Application.routes.draw do
resources :sammiches do
  get "random"
end
root :to => 'sammiches#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 %>

ルート-

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

エラー-

localhost:3000 - Routing Error

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

どんな援助でも大歓迎です。

ありがとう!

4

1 に答える 1