【问题标题】:No route matches {:action=>"show", :controller=>"controller_name"}没有路线匹配 {:action=>"show", :controller=>"controller_name"}
【发布时间】:2013-09-11 13:58:50
【问题描述】:

我总是收到这个错误,我不明白我错在哪里。我想我在控制器中拥有我需要操作的一切,路由文件中的资源和控制器操作的视图。当我收到此错误时,我将路由 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

【问题讨论】:

  • 你是否在链接中包含了id?
  • 我忘了说,我正在尝试转到新页面,所以在浏览器中我说 current_events/new,我会解决我的问题。谢谢!

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


【解决方案1】:

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

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

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

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

更新

根据您的 rake 路线

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

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

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

但请记住,除非您在此视图的控制器操作中正确定义它,否则 @event 将不起作用。

【讨论】:

  • 嗨,是的,我有类似的东西,我把它添加到我的问题中 我需要使用实例变量吗?如果是,为什么?谢谢
  • 谢谢,所以我必须在控制器中指定一个参数:id 如果我希望它链接到特定的帖子
  • @NaomiKuntinama,对 - 只需查看 rake 路由的输出,了解需要什么
  • 我不确定我是否完全理解,现在我正在尝试修复 new_current_event_path 的链接。我添加了 new_current_event_path(@current_event) 但这是语法错误?你能解释一下这是如何工作的吗?
  • 查看此“入门”指南 - 我认为它会为您解答很多问题! guides.rubyonrails.org/getting_started.html
猜你喜欢
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 2012-07-17
  • 2011-12-25
  • 2022-12-07
相关资源
最近更新 更多