没有路由匹配{:action =>“show”,:controller =>“controller_name”}

时间:2013-09-11 13:58:51

标签: ruby-on-rails ruby-on-rails-3 routing custom-routes

我总是收到这个错误,我不明白我错在哪里。我想我在控制器中需要操作,路径文件中的资源和控制器操作的视图。当我收到此错误时,我将路由current_events/new放入浏览器中。我也尝试使用resources :current_events

rake routes的输出:

   current_events GET      /current_events(.:format)              current_events#index
new_current_event GET      /current_events/new(.:format)          current_events#new
    current_event GET      /current_events/:id(.:format)          current_events#show

config/routes.rb

appname::Application.routes.draw do
  devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks"}

  resources :current_events, only: [:show, :new, :index]
end

CurrentEventsController

class CurrentEventsController < ApplicationController

  def index
      @current_event = CurrentEvent.all

      respond_to do |format|
          format.html
          format.json { render json: @current_event }
      end
  end

  def create
      @current_event = CurrentEvent.new(params[:current_event])

      respond_to do |format|
          if @current_event.save
              format.html { redirect_to @current_event, notice: 'current event was created.' }
              format.json { render json: @current_event, status: :created, location: @current_event }
          else
              format.html { render action: "new" }
              format.json {render json: @current_event.errors, status: :unprocessable_entity}
          end
      end
  end

  def new
      @current_event = CurrentEvent.new

      respond_to do |format|
          format.html
          format.json { render json: @current_event }
      end
  end

  def edit
      @current_event = CurrentEvent.find(params[:id])
  end

  def destroy
      @current_event = CurrentEvent.find(params[:id])
      @current_event.destroy

      respond_to do |format|
          format.html { redirect_to current_event_url}
          format.json { head :no_content }
      end
  end

  def show
      @current_event = CurrentEvent.find(params[:id])

      respond_to do |format|
          format.html
          format.json{ render json: @current_event}
      end
  end
end

1 个答案:

答案 0 :(得分:0)

  

我忘了说,我正在尝试进入新页面所以在浏览器中我说current_events / new

如果链接在页面上不起作用,它仍会抛出错误。

在您看来,链接应如下所示:

<%= link_to "name of current event", current_event_path(@current_event) %>

更新

基于您的佣金路线

current_event GET      /current_events/:id(.:format) #note ":id"

当您尝试查看特定的当前事件时,您需要传递:id这是有意义的 - 如果您尝试呼叫特定的人,则需要使用他们的电话号码。所以你的代码应该是这样的:

<%= link_to 'Back', current_event_path(@event) %>  

但请记住,@event除非您在此视图的控制器操作中正确定义,否则{{1}}将无效。