English 中文(简体)
按数据库价值分类的过滤控制器状况?
原标题:Filter controller condition by value in database?

我在我的控制人员之一(在铁路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

我试图从这里找到最佳操作方法,基本上每个用户都有自己的目的地代码(在这种情况下,是1245678页)。

用户能否在能够传入控制器的模式中具有价值?

实例

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

我确信,上述法典将不奏效,但要实现同样的东西,将做些什么?

2. 更新资料,少有新资料:

我有两个模式,一个是电话,另一个是用户。

我想这样做:

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

如果:目的地是呼吁模式的一部分,目的地是用户模式的一部分。 每个用户都有不同的目的地——id值。

<>http://europa-eu>

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

<>strong>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

<>唱名表决>

class Call < ActiveRecord::Base
end
最佳回答

您可在<条码>第阵列中将目的地传送给控制员。 这样,你们就能够像控制者一样获得控制。

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 
问题回答

暂无回答




相关问题
What do you have in your Model class?

What do you have in your model classes. Generally if i use any framework or any library (Zend Framework) , my classes only have variable which is table name . I know that in complicated applications ...

Rails Model With Aggregrate Data (not backed by a table)

Id like to create a model in rails that does not correlate to a table in the database. Instead the model should dynamically pull aggregrate data about other models. Example: I have a Restaurant ...

Trouble changing databases for my models in codeigniter

I m making a website with two different databases. Let s say one is DB1, and the other is DB2. I ve set up my database.php in the config folder, so they each have the correct host/password/username/...

EMAIL server FSP model

For my assignment I need to develop FSP model for email server and client. I manage to write simple model which describes one user, server and his mailbox, but I am having problems changing this ...

Rails Custom Model Functions

I m in a databases course and the instructor wants us to develop an e-commerce app. She said we can use any framework we like, and now that we re halfway through the semester she decided that Rails ...

热门标签