【问题标题】:Routing Error "No route matches" 'new' path路由错误“没有路由匹配”“新”路径
【发布时间】:2013-01-02 14:16:00
【问题描述】:

我有一个User 模型和一个Storefront 模型。

由于某种原因,当我尝试转到 /storefronts/new 时,我收到此错误:

路由错误 没有路线匹配 {:action=>"edit", :controller=>"storefronts", :id=># Storefront id: nil, name: nil, user_id: 4, created_at: nil, updated_at:无,描述:无,位置:无>}

花了 3 个小时试图弄清楚这一点。昨天还在工作。。 为什么它说 :action=>"edit" 当它是“新”操作时?

这是我的代码:

class Storefront < ActiveRecord::Base
  attr_accessible :name, :location, :description
  belongs_to :user
end

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password
  has_one :storefront
end

class StorefrontsController < ApplicationController
  before_filter :check_auth, only: [:new, :edit, :update]

  def index
    @storefronts = Storefront.all
  end

  def new
    @storefront = current_user.build_storefront
  end

  def create
    @storefront = current_user.build_storefront(params[:storefront])
    if @storefront.save
      redirect_to edit_storefront_path(@storefront)
    else
      render 'new'
    end
  end

def show
    @storefront = Storefront.find(params[:id])
  end

  def edit
    @storefront = Storefront.find(params[:id])
  end

  def update
    @storefront = Storefront.find(params[:id])
    if @storefront.update_attributes(params[:storefront])
      redirect_to @storefront
    else
      render 'edit'
    end
  end
end


Pbf::Application.routes.draw do
  resources :sessions, :only => [:new, :create, :destroy]
  resources :users
  resources :storefronts

  root :to => 'storefronts#index'
  match '/signup',  to: 'users#new'
  match '/login', to: 'sessions#new'
  match '/logout', to: 'sessions#destroy'
end

我正在使用的链接(这是问题所在):

  <% if current_user.storefront %>
    <%= link_to "Manage Storefront", edit_storefront_path(current_user.storefront) %>
  <% else %>
    <%= link_to "Open Storefront!", openstore_path %>
  <% end %>

提前致谢!

编辑: 我的rake routes

   sessions POST   /sessions(.:format)             sessions#create
new_session GET    /sessions/new(.:format)         sessions#new
    session DELETE /sessions/:id(.:format)         sessions#destroy
      users GET    /users(.:format)                users#index
            POST   /users(.:format)                users#create
   new_user GET    /users/new(.:format)            users#new
  edit_user GET    /users/:id/edit(.:format)       users#edit
       user GET    /users/:id(.:format)            users#show
            PUT    /users/:id(.:format)            users#update
            DELETE /users/:id(.:format)            users#destroy
storefronts GET    /storefronts(.:format)          storefronts#index
            POST   /storefronts(.:format)          storefronts#create  
new_storefront GET    /storefronts/new(.:format)   storefronts#new 
edit_storefront GET    /storefronts/:id/edit(.:format) storefronts#edit
 storefront GET    /storefronts/:id(.:format)      storefronts#show
            PUT    /storefronts/:id(.:format)      storefronts#update
            DELETE /storefronts/:id(.:format)      storefronts#destroy

【问题讨论】:

  • 查看rake routes 的输出,了解与此路径相关的任何明显线索。
  • 我会在我的rake routes编辑
  • 模板中的哪一行出现了该错误?而且,当您查看日志时,会触发什么操作?只需确保您按预期执行新操作即可。

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2


【解决方案1】:

在我看来,您的观点有误。 app/views/storefronts/new.html.erb 错误地引用了 edit_storefront_path 而没有将 storefront_id 作为参数传递。你可能想要storefronts_path

【讨论】:

  • 你到底是什么意思?即使我删除了app/views/storefronts/new.html.erb 文件中的所有内容,我也会遇到同样的错误。我对 storefront_path 进行了项目范围的搜索,它只弹出 3 次(edit_storefront_path 两次,new_storefront_path 一次)
  • 已在上面编辑。在此视图中是否引用了这两次出现的 edit_storefront_path 中的任何一个?其中一个没有获得有效的店面。
  • 是的!这就是问题所在。我编辑了我的问题以显示它,但我基本上有一个条件,在那里放置了一个无效的edit_storefront_path。我应该把逻辑扔给助手吗?请原谅我太密集了,但您能解释一下为什么视图中有一个无效的edit_storefront_path 会弄乱new_storefront_path 链接吗?
  • 问题是这是你的条件,current_user.storefront 正在测试店面对象,它存在是因为你已经在你的控制器中构建了它,但它还没有保存,所以它是 id是nil。当您调用 edit_storefront_path(current_user.storefront) 时,edit_storefront_path 助手正在寻找具有非零 id 的对象。错误消息准确地告诉你,在这个位`:id=># Storefront id: nil`
【解决方案2】:

new_storefronts_path是集合路由,如果你传递单数形式,如果你没有声明任何命名路由,它被认为是成员路由, 这就是为什么路由选择编辑操作并且关于 id 4 是你没有按照约定传递 id 所以nil.object_id 是 4。

【讨论】:

  • 感谢您的回答!我试过这个:match '/openstore', to: 'storefronts#new' 和&lt;%= link_to "New Storefront", openstore_path %&gt; 但我仍然得到完全相同的错误。我还能尝试什么?
【解决方案3】:

也许问题是路由器无法从某种“:id”中找出“新”。尝试在您的编辑路由声明中添加约束

get 'storefronts/:id/edit' => 'storefronts#edit', :id => /[\d]*/

那么只有数字 id 会被解释为编辑操作的调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多