Ruby:nil的未定义方法`name':NilClass

时间:2017-07-20 06:01:57

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

我正在尝试在Windows 7 https://github.com/cheezy/puppies上安装此应用。

我不是开发人员,我只是按照以下步骤进行安装:

在Windows 7上安装了Ruby版本ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32]

步骤:

  1. 从上面链接下载zip
  2. 在puppies-master内部进行捆绑更新,然后捆绑安装。
  3. 然后是rails s
  4. 启动localhost:3000
  5. 错误ruby : unsupported parameters: :order。我修好了        改变这一行

    @puppies = Puppy.paginate :page =>params[:page], :order => 'name', :per_page => 4到这一行 @puppies = Puppy.order(:name).paginate(page: params[:page], per_page: 4)

  6. 错误消失了,在localhost:3000上它显示了我启动的应用

  7. 但小狗(数据)不存在,所以我做了

    bundle && rake db:create && rake db:migrate然后rake db:seed

  8. 刷新本地主页并显示所有小狗但在我无法完成交易并点击Complete the adoption按钮后,它会给我一个错误。

    undefined method 'name' for nil:NilClass

  9. 如果您只是在SOF中键入undefined method 'name' for nil:NilClass,您将获得如此多的解决方案,但它们似乎与那些不同,因为没有一个对我有效。

    这就是日志所说的:

    Started GET "/orders/new" for 127.0.0.1 at 2017-07-20 10:10:51
    Processing by OrdersController#new as HTML
      Cart Load (0.0ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 1]]
      CACHE (0.0ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 1]]
    Completed 500 Internal Server Error in 3ms
    
    NoMethodError (undefined method `name' for nil:NilClass):
      app/controllers/orders_controller.rb:23:in `new'
    
    
      Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
      Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
      Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (242.5ms)
    
    
    Started GET "/orders/new" for 127.0.0.1 at 2017-07-20 11:20:21 
    Processing by OrdersController#new as HTML
      Cart Load (0.0ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 1]]
      CACHE (0.0ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 1]]
    Completed 500 Internal Server Error in 3ms
    
    NoMethodError (undefined method `name' for nil:NilClass):
      app/controllers/orders_controller.rb:23:in `new'
    
    
      Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
      Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
      Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (252.0ms)
    

    修改

    orders_controller.rb

    class OrdersController < ApplicationController
      skip_before_filter :authorize, :only => [:new, :create]
    
      def index
        @orders = Order.paginate :page=>params[:page], :order=>'created_at desc',
        :per_page => 10
    
        respond_to do |format|
          format.html # index.html.erb
        end
      end
    
      def show
        @order = Order.find(params[:id])
    
        respond_to do |format|
          format.html # show.html.erb
        end
      end
    
      def new
        @cart = current_cart
        if current_cart.adoptions.empty?
          redirect_to agency_url, :notice => "Your cart is empty"
          return
        end
    
        @order = Order.new
    
        respond_to do |format|
          format.html # new.html.erb
          format.json { render :json => @order }
        end
      end
    
      def edit
        @order = Order.find(params[:id])
      end
    
      def create
        @order = Order.new(params[:order])
        @order.add_adoptions_from_cart(current_cart)
    
        respond_to do |format|
          if @order.save
            Cart.destroy(session[:cart_id])
            session[:cart_id] = nil
            format.html { redirect_to(agency_url, :notice => 'Thank you for adopting a puppy!') }
            format.json { render :json => @order }
          else
            format.html { render :action => "new" }
          end
        end
      end
    
      def update
        @order = Order.find(params[:id])
    
        respond_to do |format|
          if @order.update_attributes(params[:order])
            format.html { redirect_to(@order, :notice => 'Order was successfully updated.') }
          else
            format.html { render :action => "edit" }
          end
        end
      end
    
      def destroy
        @order = Order.find(params[:id])
        adoption = @order.adoptions.first
        adoption.delivered_on = Time.now
        adoption.save
    
        respond_to do |format|
          format.html { redirect_to(orders_url, :notice => "Please thank #{@order.name} for the order!") }
        end
      end
    end
    

    EDIT2:

    current_card方法 -

    class ApplicationController < ActionController::Base
      before_filter :authorize
      protect_from_forgery
    
      private
    
      def current_cart
        Cart.find(session[:cart_id])
      rescue ActiveRecord::RecordNotFound
        cart = Cart.create
        session[:cart_id] = cart.id
        cart
      end
    
      protected
    
      def authorize
        unless User.find_by_id(session[:user_id])
          redirect_to login_url, :notice => "Please log in"
        end
      end
    end
    

0 个答案:

没有答案