0

我有一个自定义重新排序操作,它通过 ajax 创建新项目。对站点的其他部分进行一些更改后,新建操作不起作用。

当我转到页面categories/reorder时,或者categories/new它正确加载页面时,但前提是我注释掉该行@category = Category.new()

一旦我尝试设置@category = Category.new(), @category = Category.new(:parent_id => params[:parent_id]), 或者@category = Category.new它会给我错误消息:

No route matches {:action=>"edit", :controller=>"categories", :id=>#<Category _id: 515ee18c10188f64fb000001, created_at: nil, updated_at: nil, deleted_at: nil, ancestry: nil, user_id: nil, name: nil, description: nil, image: nil, lock: nil, _slugs: []>}

我删除了我的数据库,以防有一些条目以某种方式给了我这个错误,但是在再次登录时它仍然给我这个消息。

这是我的路线文件:

  match '/auth/:provider/callback' => 'sessions#create'
  match '/auth/failure' => 'sessions#failure'
  match '/signout' => 'sessions#destroy', :as => :signout
  match '/signin' => 'sessions#new', :as => :signin

  match "reorder/symbols" => "categories#reorder", :via => :get, :as => :reorder_symbols
  resources :pages do
    resources :blocks do
      collection { post :sort }
    end
  end


  get 'symbols/:id/search/', to: 'categories#search', as: :sift_meanings

  get "logout" => "sessions#destroy", :as => "logout"
  get "login" => "sessions#new", :as => "login"
  get "signup" => "users#new", :as => "signup"
  resources :users
  resources :sessions
  match "meanings/:id/delete" => "meanings#destroy", :via => :get, as: :delete_meaning

  match "symbols/gallery/:id" => "categories#gallery", :via => :get
  resources :symbols, :as => :categories, :controller => :categories do
    resources :meanings
    collection {post :sort}
  end

  root :to => 'categories#index'

控制器文件:

class CategoriesController < ApplicationController
  helper :lego
  helper :meanings

  def index
    @categories = Category.all
    if params[:id]
      @categories = Category.find(params[:id])                      #if params[:id]
      @categories = @categories.subtree.arrange(:order => 'name')
    elsif params[:view] == "alpha"
      @alphabet   = Category.all.group_by{|c| c.name[0]}
      @see_kids   = false
      @categories = @categories.sort(:name => "ASC")                            if !params[:letter]
      @categories = @categories.where(name: eval("/^#{params[:letter]}/i")).sort(:name => "ASC")     if params[:letter]
    else
      # @categories = Category.all
      @categories = @categories.arrange(:order => 'name')               #if params[:view] != "list"
    end



    respond_to do |format|
      format.html # index.html.erb
      format.js
      format.json { render json: @categories }
    end
  end

  def reorder
    if Rails.env != "production" && !request.xhr?
      flash[:info] = "Currently in #{Rails.env} mode."
    end
    @categories = Category.arrange(:order => 'name')
    # @category = Category.new
    @next = Category.count
    respond_to do |format|
      format.html # index.html.erb
      format.js
      format.json { render json: @categories }
    end
  end

  def sort
    params[:category].each do |id, attr|
      thisCat = params[:category][id]
      @category = Category.where(:_id => id).first
      if thisCat.nil? || thisCat == 'null'
        @category.parent_id = nil
      else
        @category.parent_id = thisCat.to_s
      end
      @category.save
    end
  end

  def gallery
    @categories = Category.arrange(:order => 'name')
    @category = Category.find(params[:id])
    @siblings = Category.siblings_of(params[:id])
    respond_to do |format|
      format.html # index.html.erb
      format.js
      format.json { render json: @category }
    end
  end

   def show
     @categories = Category.arrange(:order => 'name')
     @category   = Category.find(params[:id])
     @updater    = User.find(@category.user) || nil
     @siblings   = Category.siblings_of(params[:id])
     @meanings   = @category.meanings #Meaning.where(:category_id => params[:id])
     @belief_list    = []
     @culture_list   = []
     @contributors   = []
     @connotations   = []
     for meaning in @meanings
       for belief in meaning.beliefs
         @belief_list << belief 
       end
       for culture in meaning.cultures
         @culture_list << culture
       end
       @connotations << meaning.connotation
       @contributors << meaning.user
     end
     @beliefs      = @belief_list.uniq
     @cultures     = @culture_list.uniq
     @contributors = @contributors.uniq
     @connotations = @connotations.uniq

     respond_to do |format|
       format.html # show.html.erb
       format.js
       format.json { render json: @category }
     end
   end

  def search
    @categories = Category.arrange(:order => 'name')
    @category = Category.find(params[:id])

    @meanings = @category.meanings
    @meanings = @meanings.where(:beliefs => params[:beliefs])         if params[:beliefs]
    @meanings = @meanings.where(:cultures => params[:cultures])       if params[:cultures]
    @meanings = @meanings.where(:connotation => params[:connotation]) if params[:connotation]
    @meanings = @meanings.where(:user => params[:user])               if params[:user]    
  end


  # GET /categories/new
  # GET /categories/new.json
  def new
    @category = Category.new(:parent_id => params[:parent_id])
    # @category.photo.build
    respond_to do |format|
      format.html # new.html.erb
      format.js
      format.json { render json: @category }
    end
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
  end

  # POST /categories
  # POST /categories.json
  def create
    @category = Category.new(params[:category])
    @category.user = current_user
    @categories = Category.arrange(:order => :created_at)
    if @category.save
      flash[:success] = "<h4><i class=icon-ok></i> '#{@category.name}' was successfully created.</h4>"
    end
    respond_to do |format|
      if @category.save
        format.html { redirect_to @categories, notice: 'Category was successfully created.' }
        format.js
        format.json { render json: @categories, status: :created, location: @category }
      else
        format.html { render action: "new" }
        format.js 
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.json
  def update
    @category = Category.find(params[:id])
    @category.user = current_user
    flash[:success] = "<h4><i class=icon-ok></i> #{@category.name} was successfully updated.</h4>"
    respond_to do |format|
      if @category.update_attributes(params[:category])
        format.html { redirect_to @category}
        format.js
        format.json { head :no_content }
      else
        format.html { render action: "edit", error: "Unable to update '#{@category.name}'", status: :unprocessable_entity }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category = Category.find(params[:id])
    @category.destroy
    flash[:alert] = "<h4><i class=icon-warning-sign></i> Warning. You have deleted the category '#{@category.name}'.</h4>".html_safe


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

  def has_sidebar?
    self.has_children?
  end
private

end

唯一需要注意的另一件事是,我发现在恢复到较旧的工作版本时,我必须重新启动服务器,因为未正确设置较少的 Twitter Bootstrap 变量。但我认为这不应该影响这一点。

4

1 回答 1

0

如果您跟踪堆栈跟踪,它会告诉您错误在哪里。由于您提到这仅在您添加调用时才会发生@category = Category.new,所以我只能认为这是因为您正在使用@category创建编辑链接。但由于@category是一个新对象,你会得到错误。尝试查看可以找到编辑链接的视图文件。

于 2013-04-05T15:30:23.147 回答