简体   繁体   English

Rails select_tag onchange操作是否没有Ajax?

[英]Rails select_tag onchange action WITHOUT Ajax?

In my index action of the hours controller I want to be able to switch between the diplayed hours. 在小时控制器的索引操作中,我希望能够在显示的小时之间切换。 I am using a calendar_for table and inside the calendar I display the tracked times (hours) of the different user of my rails app. 我正在使用calendar_for表,并且在日历内部显示了我的Rails应用程序不同用户的跟踪时间(小时)。 As I am using a quite complex calendar structure with some calculation inside, I don't know how to extract it to a partial, so I want the onchange event, which fires in the select_tag, to reload the index action with a different parameter. 由于我使用的是一个内部比较复杂的日历结构,因此我不知道如何将其提取到部分日历中,因此我希望在select_tag中触发的onchange事件使用其他参数重新加载索引操作。

I tried 我试过了

<% form_for :hours, :url => { :action => "index" } do |f| %>
  <%= select_tag :user_id, options_from_collection_for_select(User.find(:all), :id, :name),
  { :onchange => "this.form.submit();"} %>
<% end %>

but this brings me to the new / form view (like i want to add a new record)? 但这将我带到新的/表单视图(例如我想添加新记录)? also the url just shows /hours 网址也只显示/hours

Any help would be appreciated.... 任何帮助,将不胜感激....

I basically want that after changing the select menu the url looks like /hours?user_id=<id from select_tag> 我基本上希望更改选择菜单后,URL看起来像/hours?user_id=<id from select_tag>

thanks 谢谢

I do not quite understand why do you need a form? 我不太了解您为什么需要表格? The simplest way from my opinion is update your application.js file with something like this (assuming you use jquery): 在我看来,最简单的方法是使用如下所示的方式更新application.js文件(假设您使用的是jquery):

$("#selector_id").change ( function () {
  window.location.replace('/hours?'+this.id);
});

Though probably there is some pretty rails helper for that. 尽管可能有一些漂亮的Rails助手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM