undefined方法`save'为1:Fixnum

时间:2014-09-29 08:35:32

标签: ruby ruby-on-rails-4 methods save nomethoderror

我做了一些与模型相关的变量,我想保存与控制结构相关的新变量,但我不能。它说“NoMethodError - 未定义方法`保存'为1:Fixnum:”。

我想要的是该程序获得4个人的功能,它将显示该成员的成员名称。第五名成员将在下一组。

任何人都可以解决这个问题吗?

或者如果您需要更多信息,请告诉我。

由于

这是Waitinglists_controller

    class WaitinglistsController < ApplicationController
        before_action :authenticate

        def new
            @waitinglist = current_user.created_waitinglists.build
        end

        def create
            @waitinglist = current_user.created_waitinglists.build(waitinglist_params)
            if @waitinglist.save
                redirect_to waitinglist_waiting_path(@waitinglist, @owner)
            else
                render :new
            end
        end
    def waiting

        @group_number = Waitinglist.select(:count_number).last 
        @already_group_people = Waitinglist.where(count_number: @group_number).count
        @current_person_group_number = current_user.created_waitinglists.select(:count_number) 
        @current_group_people = Waitinglist.where(count_number: @current_person_group_number).count 

        case @already_group_people

        when 0
            @current_person_group_number = 1 
            @current_person_group_number.save 

        when 1..2 
            @current_person_group_number = @group_number 
            @current_person_group_number.save 

        when 3 
            @current_person_group_number = @group_number 
            @current_person_group_number.save 
            redirect_to show_waitinglist_path 

        when 4 
            group_number += 1 


            @current_person_group_number = @group_number 
                @current_person_group_number.save 
            end

    end

def show


    @current_person_group_number = current_user.created_waitinglist.select(:count_number)



        @matched_people = Waitinglist.find(count_number: @current_person_group_number)

        @matched_people == 0 if @matched_people = nil
    end 



    private

    def created_by?(user)
        return false unless user
        owner_id == user.id
    end

    def waitinglist_params
        params.require(:waitinglist).permit(:look_like, :id)
    end
end

这是用户登录的会话控制器

class SessionsController < ApplicationController

    def create
        user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])
        session[:user_id] = user.id
        redirect_to root_path
    end

    def destroy
        reset_session
        redirect_to root_path
    end


end

应用程序contrtoller就是这个

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception



  helper_method :current_user, :logged_in?

    private

    def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end

    def logged_in?
        !!session[:user_id]
    end

    def authenticate
        return if logged_in?
        redirect_to root_path
    end

end

代码适用于型号

class Waitinglist < ActiveRecord::Base
    belongs_to :waiting_person, class_name: 'User'
    after_initialize :init

    def init
      self.count_number  ||= 1           #will set the default value only if it's nil
    end
end

class User < ActiveRecord::Base
    has_many :created_waitinglists, class_name: 'Waitinglist', foreign_key: :owner_id

    def self.find_or_create_from_auth_hash(auth_hash)
        provider = auth_hash[:provider]
        uid = auth_hash[:uid]
        name = auth_hash[:info][:name]
        image_url = auth_hash[:info][:image]

        User.find_or_create_by(provider: provider, uid: uid) do |user|
            user.nickname = name
            user.image_url = image_url
        end
    end
end

2 个答案:

答案 0 :(得分:3)

对我来说很清楚。您在save上调用了@current_person_group_number,这是Fixnum的实例,因此未定义save方法。

答案 1 :(得分:0)

不幸的是,你写的代码没什么意义,很难理解你在这里想做什么。

首先,您过度使用实例变量。如果你不是在另一种方法中使用它们(很难说你没有发布你的其他课程)

其次,您过度使用select方法。

@group_number = Waitinglist.select(:count_number).last 

它只是在查询模型的数据库时更改SELECT语句,但它仍然返回模型,而不是数字或字段值。所以@group_number不是数字 - 它是一个WaitingList实例。如果你想要一个数字:

group_number = WaitingList.last.count_number

(现在发布的问题可能会在一秒钟后关闭。如果不会发生,将在稍后更新)

您只能保存ActiveRecord对象,这意味着您需要将其保存在某处。显然你想要更新@current_person_group_number,但是你不能重新分配这个变量来做这个伎俩。您必须获取整个模型,更改其属性,然后保存模型。它看起来像是:

current_waiting_list = current_user.created_waitinglists.last    # This seems to be a collection, you need to tell here which waiting list you want to get from this collection
current_waiting_list.count_number += 1
current_waitin_list.save

我的最后一点是 - 请查看act_as_list gem。既然您正在创建等待名单,那么它就是您必备的宝石。