新操作不起作用,调用Category.new()时路由错误

时间:2013-04-05 14:58:29

标签: ruby-on-rails ajax mongoid

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

当我转到页面categories/reordercategories/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变量。但我认为这不应该影响到这一点。

1 个答案:

答案 0 :(得分:0)

如果您遵循堆栈跟踪,它将显示错误的位置。由于您提到只有当您添加对@category = Category.new的调用时才会发生这种情况,我只能认为这是因为您使用@category来创建编辑链接。但由于@category是一个新对象,你会收到错误。尝试查看可以找到编辑链接的视图文件。