【问题标题】:Call a Method Model inside Controller在 Controller 中调用方法模型
【发布时间】:2013-11-29 03:33:43
【问题描述】:

我有以下型号; (app/models/student_inactivation_log.rb)

class StudentInactivationLog < ActiveRecord::Base
    belongs_to :student
    belongs_to :institution_user
    belongs_to :period

    validates_presence_of :student_id, :inactivated_on, :inactivation_reason

    INACTIVATION_REASONS = [{ id: 1, short_name: "HTY", name: "You didn't study enough!"},
                            { id: 2, short_name: "KS", name: "Graduated!"},
                            { id: 3, short_name: "SBK",name: "Other Reason"}]

    Class methods
        class << self
        def inactivation_reason_ids
            INACTIVATION_REASONS.collect{|v| v[:id]}
        end

        def inactivation_reason_names
            INACTIVATION_REASONS.collect{|v| v[:name]}
        end

        def inactivation_reason_name(id)
          INACTIVATION_REASONS.select{|t| t[:id] == id}.first[:name]
        end

        def inactivation_reason_short_name(id)
          INACTIVATION_REASONS.select{|t| t[:id] == id}.first[:short_name]
        end

        def inactivation_reason_id(name)
          INACTIVATION_REASONS.select{|t| t[:name] == name}.first[:id]
        end
    end

    # Instance methods
    def inactivation_reason_name
        self.class.inactivation_reason_name(self.inactivation_reason)
    end

    def inactivation_reason_short_name
        self.class.inactivation_reason_short_name(self.inactivation_reason)
    end

    def inactivation_reason_id
        self.class.inactivation_reason_id(self.inactivation_reason)
    end
end

我想从我的控制器中调用这些停用原因,即 app/controllers/student/session_controllers.rb 文件:

class Student::SessionsController < ApplicationController
    layout 'session'

    def create
        student = Student.authenticate(params[:student_number], params[:password])
        if student.active
            session[:student_id] = student.id
            redirect_to student_main_path, :notice => 'Welcome!'
        elsif (student and student.student_status == 3) or (student and !student.active)
            flash.now.alert = "You can't login because #REASON_I_AM_TRYING_TO_CALL"
            render 'new'
        else
            ....
    end
end

如果学生无法登录,我想向他们展示他们在系统上的停用原因。

如何从这个控制器文件中调用我的 INACTIVATION_REASONS?有可能吗?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails methods controller


    【解决方案1】:

    这只是一个常数,因此您可以在任何地方将其称为常数。

    StudentInactivationLog::INACTIVATION_REASONS
    

    更新

    我意识到实际上你想要的是使用保存在 db 中的原因代码或短名称来表示字符串。

    如果是这样,我建议您直接使用短名称作为哈希。对于这种轻量级的情况,“id”看起来是多余的。

    INACTIVATION_REASONS = {"HTY"=>"You didn't study enough!",
                            "KS"=>"Graduated!",
                            "SBK"=>"Other Reason"}
    
    validates :inactivation_reason, inclusion: { in: INACTIVATION_REASONS.keys,
      message: "%{value} is not a valid short name" }
    
    def full_reason_message
      INACTIVATION_REASONS[self.inactivation_reason]
    end
    

    然后,在控制器中显示完整的原因信息

    reason = @student.full_reason_message
    

    这就是想法。我还没有检查你的其他型号代码。您需要将原因保存为短名称而不是 id,如果您决定以这种方式使用它,则需要修改/删除一些代码。

    【讨论】:

    • 当我这样称呼它时:flash.now.alert StudentInactivationLog::INACTIVATION_REASONS.collect{|v| v[:name]} 只是列出所有停用原因,而不是与学生相关的原因。
    • 似乎对我不起作用:NoMethodError in Student::SessionsController#create undefined method `full_reason_message' for nil:NilClass
    • 这不是我的方法的问题。您的学生实例为零,这就是原因。你需要找出原因。
    猜你喜欢
    • 2012-06-23
    • 1970-01-01
    • 2015-11-18
    • 2019-10-08
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多