0

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

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

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

花了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
4

3 回答 3

2

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

于 2013-01-18T19:55:51.973 回答
0

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

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

然后只有数字 ID 将被解释为对编辑操作的调用。

于 2013-01-18T19:53:34.907 回答
0

new_storefronts_path是集合路线,如果您通过单数形式,那么如果您没有声明任何命名路线,则将其视为成员路线,这就是为什么路线选择编辑操作并且关于 id 4 是您没有按照约定传递 id 所以nil.object_id是 4 .

于 2013-01-18T19:50:43.337 回答