按数据库中的值过滤控制器条件?

发布于 2024-12-26 17:18:48 字数 2415 浏览 0 评论 0原文

我的一个控制器(在 Rails 3.1 应用程序中)中有以下代码,该代码运行良好:

def index
    #@calls = Call.all
    @calls = Call.where(:destination => '12345678').limit(25)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @calls }
    end
end

我正在尝试找出从这里继续的最佳方法,基本上每个用户都有自己的目标代码(在本例中为 12345678 )。

用户是否可以在模型中拥有可以传递到控制器中的值?

举个例子,

def index
    #@calls = Call.all
    @calls = Call.where(:destination => '<% @user.destination %>').limit(25)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @calls }
    end
end

我意识到上面的代码不起作用,但是实现相同目标的解决方法是什么?

更新一点更多信息:

我有两种模型,一种是呼叫,另一种是用户。

我希望能够做这样的事情:

@calls = Call.where(:destination => @user.destination_id).limit(25)'

其中 :destination 是 Calls 模型的一部分,destination_id 是 users 模型的一部分。每个用户都有不同的destination_id值。

路由

Outofhours::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config

get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :users
resources :sessions

resources :calls
root :to => 'dashboards#index'
resources :dashboards
end

用户模型

class User < ActiveRecord::Base
  attr_accessible :email, :company, :destination_id, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email
  validates_uniqueness_of :company
  validates_uniqueness_of :destination_id

  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end

呼叫模型

class Call < ActiveRecord::Base
end

I have the following code in one of my controllers (in a Rails 3.1 application) which works well:

def index
    #@calls = Call.all
    @calls = Call.where(:destination => '12345678').limit(25)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @calls }
    end
end

I'm trying to work out the best way of proceeding from here, basically each user has their own destination code (in this case it's 12345678).

Is it possible for the users to have a value in a model which can be passed into the controller?

An example

def index
    #@calls = Call.all
    @calls = Call.where(:destination => '<% @user.destination %>').limit(25)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @calls }
    end
end

I realise that the above code wouldn't work but what would be a workaround to achieve the same thing?

Update with a little more information:

I have two models, one is calls and the other is users.

I want to be able to do something like this:

@calls = Call.where(:destination => @user.destination_id).limit(25)'

Where :destination is part of the Calls model and destination_id is part of the users model. Each user has a different destination_id value.

Routes

Outofhours::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config

get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :users
resources :sessions

resources :calls
root :to => 'dashboards#index'
resources :dashboards
end

user model

class User < ActiveRecord::Base
  attr_accessible :email, :company, :destination_id, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email
  validates_uniqueness_of :company
  validates_uniqueness_of :destination_id

  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end

call model

class Call < ActiveRecord::Base
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2025-01-02 17:18:48

您可以将目标传递给 params 数组中的控制器。这样你就可以像这样在控制器中访问它

def index     
  #@calls = Call.all
  @calls = Call.where(:destination => current_user.destination_id).limit(25)

  respond_to do |format|       
    format.html # index.html.erb
    format.json { render :json => @calls }     
  end
end 

You could pass the destination to the controller in the params array. This way you could access it in the controller like this

def index     
  #@calls = Call.all
  @calls = Call.where(:destination => current_user.destination_id).limit(25)

  respond_to do |format|       
    format.html # index.html.erb
    format.json { render :json => @calls }     
  end
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文