Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails更改记录值_Ruby On Rails_Ruby On Rails 3_Attributes_Ruby On Rails 3.2_Records - Fatal编程技术网

Ruby on rails Rails更改记录值

Ruby on rails Rails更改记录值,ruby-on-rails,ruby-on-rails-3,attributes,ruby-on-rails-3.2,records,Ruby On Rails,Ruby On Rails 3,Attributes,Ruby On Rails 3.2,Records,我在postgresql中有一条用户状态记录,它是布尔值,属性为“true”和“false”。我想将“true”显示为“active”,将“false”显示为“inactive”。我如何使用查询或任何要添加到模型中的东西来实现这一点 控制器: def index @users = User.reorder("id ASC").page(params[:page]).per_page(10) @count = 0 end 型号: class Us

我在postgresql中有一条用户状态记录,它是布尔值,属性为“true”和“false”。我想将“true”显示为“active”,将“false”显示为“inactive”。我如何使用查询或任何要添加到模型中的东西来实现这一点

控制器:

     def index
     @users = User.reorder("id ASC").page(params[:page]).per_page(10)
     @count = 0
     end 
型号:

    class User < ActiveRecord::Base
    has_many :orders
    has_many :order_statuses

     attr_accessible :first_name, :last_name, :email, :password,
  :password_confirmation, :code

     validates :first_name, presence: true
     validates :last_name, presence: true
     VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
      validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },          uniqueness: { case_sensitive: false }
     validates :password, length: { minimum: 6}

    has_secure_password
    before_save { self.email = email.downcase }
     before_create :create_remember_token

    def User.new_remember_token
    SecureRandom.urlsafe_base64
    end

    def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
    end

     private

     def create_remember_token
     self.remember_token = User.encrypt(User.new_remember_token)
      end
        end
class用户
在您的模型中添加此方法,当您调用
@user.status
时,它将显示“活动”或“非活动”


希望能有所帮助。谢谢将此方法添加到您的模型中,当您调用
@user.status
时,它将显示“活动”或“非活动”


希望能有所帮助。谢谢

如果我理解正确,您希望向用户显示“活动”而不是“真”,显示“非活动”而不是“假”

您可以在所有视图中执行类似操作:

@user.status? ? 'active' : 'inactive'
或者,如果您在很多地方都需要它,您可以编写一个助手:

module UserHelper
  def status_text(user)
    @user.status? ? 'active' : 'inactive'
  end
end

# and call it from your views like this:

<%= status_text(@user) %>

如果我理解正确,您希望向用户显示“活动”而不是“真”,以及“非活动”而不是“假”

您可以在所有视图中执行类似操作:

@user.status? ? 'active' : 'inactive'
或者,如果您在很多地方都需要它,您可以编写一个助手:

module UserHelper
  def status_text(user)
    @user.status? ? 'active' : 'inactive'
  end
end

# and call it from your views like this:

<%= status_text(@user) %>

上面的代码给了我无法识别的错误,所以我对其进行了一些修改:def User.status self.status?“Active”:“Inactive”“最终的问题是,它仍然没有将值更改为活动。感谢您的帮助。您将其定义为类方法,无需执行此操作。请把你的错误贴在这里。问题解决了。我把函数直接写进了索引页。又是Thanx.索引页?你是说索引视图。永远不要在视图中编写函数。上面的代码给了我无法识别的错误,所以我对其进行了一些修改:def User.status self.status?“活动”:“非活动”的最终问题是它仍然没有将值更改为活动。感谢您的帮助。您将其定义为类方法,无需执行此操作。请把你的错误贴在这里。问题解决了。我把函数直接写进了索引页。又是Thanx.索引页?你是说索引视图。永远不要在视图中编写函数。