我正在尝试创建一个选择框,显示我的
ejecutive_name
和
last_name
从我的“策略”视图中的表Ejecutives中,但我需要创建一个搜索按钮来从我选择的Ejecutive中获取参数
我的模特有一种关系:
class Policy < ActiveRecord::Base
unloadable
belongs_to :ejecutive
has_many :policy
end
class Ejecutive < ActiveRecord::Base
has_many :policies
end
我的桌子之间的关系
ejecutive_id
:
class CreateEjecutives < ActiveRecord::Migration
def self.up
create_table :ejecutives do |t|
t.string :name,:null=>false
t.string :lastname1,:null=>false
t.timestamps
end
end
def self.down
drop_table :ejecutives
end
end
class CreatePolicies < ActiveRecord::Migration
def self.up
create_table :policies do |t|
t.string :num_policy, :null=>false
t.integer :ejecutive_id
t.timestamps
end
end
def self.down
drop_table :policies
end
end
这是我的控制器:
class PolicyManagement::PolicyController < ApplicationController
@ejecutives = Ejecutive.find(:all)
@policies = Policy.find(:all)
end
这是我的观点:
Select Ejecutive:
%= select_tag 'ejecutives',"<option value=\"\">Seleccione</option>"+options_for_select(@ejecutives.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]})%>
Results
<% @policies.each do |policy| %>
<p> <%= policy.num_policy%> </p>
<p> <%= policy.ejecutive.name %> </p>
<p> <%= policy.ejecutive.last_name %> </p>
<% end %>
我试过了
<% form_tag :controller=>"policy_management/policy",:action =>"generate_print_ejecutive_comercial", :method => 'get' do %>
<p>
<%= text_field_tag :search,params[:search] %>
<%= select_tag "Ejecutives", options_from_collection_for_select(@ejecutives, "id", "name") %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
我使用的是Rails 2.3.5。
有人知道这个问题吗?我真的很感激你的帮助。