我的tasks_controller.rb中有一个方法
def index
@tasks = Task.all
end
我使用表来显示@tasks
<table>
<thead>
<th>Name</th>
<th>Content</th>
</thead>
<tbody>
<% @tasks.each do |task| %>
<tr>
<td><%= task.name %></td>
<td><%= task.content %></td>
</tr>
<% end %>
</tbody>
</table>
我的下拉列表
<%= f.label 'Task' %>: <%= f.select :locate_id, Locate.all.collect { |l| [ l.locate_name, l.id ] }, {:prompt => 'choose'}, :onchange => '$.get(#{/index})' %>
当选择dropdownlist时,我希望这个表可以使用选定的值自动刷新(只刷新表),方法如下:
@tasks = Task.where('locate_id = ?', params[:task][:locate_id])
我该怎么做?我搜索这个问题,看起来好像使用onchange
和javascript
可以做我想做的事,有人可以给我一些提示,我是javascript的新手......
感谢。
我添加:'data-remote'=&gt; 'true',:'data-type'=&gt; 'HTML'到我的select
,我得到了正确的响应(我选择的值来搜索),就像我进入页面一样,但页面没有刷新。有什么问题?
Started GET "/tasks/new" for IP at 2015-08-10 10:24:15 +0800
Processing by TasksController#new as HTML
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 2 LIMIT 1
Locate Load (0.2ms) SELECT `locates`.* FROM `locates`
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE (manager_id = 2)
Beacon Load (0.5ms) SELECT `beacons`.* FROM `beacons` WHERE (account_id = 2) AND (beacon_status < 3)
Category Load (0.3ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Rendered tasks/_form.html.erb (9.1ms)
Rendered tasks/new.html.erb within layouts/application (9.9ms)
Rendered common/_navbar.html.erb (0.5ms)
Rendered common/_footer.html.erb (0.1ms)
Completed 200 OK in 64ms (Views: 60.4ms | ActiveRecord: 1.8ms)
Started GET "/tasks/new?task%5Blocate_id%5D=1" for IP at 2015-08-10 10:24:19 +0800
Processing by TasksController#new as HTML
Parameters: {"task"=>{"locate_id"=>"1"}}
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 2 LIMIT 1
Locate Load (0.2ms) SELECT `locates`.* FROM `locates`
Account Load (0.2ms) SELECT `accounts`.* FROM `accounts` WHERE (manager_id = 2)
Beacon Load (0.3ms) SELECT `beacons`.* FROM `beacons` WHERE (account_id = 2) AND (beacon_status < 3) AND (locate_id = '1')
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Rendered tasks/_form.html.erb (7.8ms)
Rendered tasks/new.html.erb within layouts/application (14.5ms)
Rendered common/_navbar.html.erb (0.6ms)
Rendered common/_footer.html.erb (0.0ms)
Completed 200 OK in 85ms (Views: 82.4ms | ActiveRecord: 1.5ms)
答案 0 :(得分:0)
您可以使用更改事件。从下拉列表中选择选项时,请编写事件处理程序以捕获此事件(请参阅this link)。在处理程序函数中,为您的服务器创建Ajax request。 Ajax请求有一个成功处理程序,它基本上是一个在请求成功完成时执行的函数。在此函数中,其中一个参数将是服务器发送的数据,并根据需要动态更新表。您可能需要在服务器中使用JSON API。标准的HTML响应不会。