Asscociate action to on_change for form.select in Rails?

the following is my dropdown to select chat rooms:

<% form_for :chat_room do |form| %> <%= form.select(:title, @chat_rooms.collect! {|x| x.title},{:include_blank =>'select a chat room'}) %> <% end %>

My routes are:

ActionController::Routing::Routes.draw do |map| map.resource :account, :controller => "users" map.resources :poll map.resources :users,:has_many => :stories map.resources :chat_rooms ,:member => {:create_message => :post},:collection => {:ajax_updater=>:get} map.logout 'logout', :controller => 'user_sessions', :action =>'destroy' map.resource :user_session map.resource :welcome map.connect '/', :controller => 'welcome', :action => 'index' map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end

On changing values in the drop down I want to navigate to a particular chatroom chosen. How do I do that?

-------------Problems Reply------------

one (simple) way would be to add the :onchange parameter to the select helper

form.select(:title, @chat_rooms.collect! {|x| [x.title, x.id] },{:include_blank =>'select a chat room'}, :onchange => 'window.location.href = "chat_rooms/" + this.getAttribute("value")')

This is just a variation of the approach that andi has used above but...

The pattern I typically use now is to write a simple javascript function that will load the selected path:

CHATROOM.display_selected = function(id, base_path)
{
window.location = base_path + '?id=' + id;
}

This function is then called by the onchange of the select tag:

select_tag("chat_room",options_for_select(chat_rooms, selected), :onchange => "CHATROOM.display_selected(this.value, '#{chat_rooms_path}')")

Where the path variable is the base path for chat_rooms.

Assuming you're using jQuery (and who isn't these days), something like this would work:

ERB View (corrected from above):

<% form_for :chat_room do |form| %>
<%= form.select(:title, @chat_rooms.map { |c| [c.title, c.id] }, {:include_blank =>'select a chat room'}) %>
<% end %>

JS:

$(document).ready(function () {
$("#chat_room_title").change(function(event) {
window.location = "/chat_rooms/" + $("#chat_room_title").val() + "/";
});
});

The Prototype code would be very similar.

Just a quick note, I know you didn't ask about it. But when you are working with chats be sparse with ajax polling to check for new chats. If you weren't doing so already check out Juggernaut.

Category:ruby on rails Views:2 Time:2009-04-06

Related post

  • I have an ASP.net form on a website. I need to edit its actions based on the user selections, and make the radio button list allow multiple choices 2011-06-16

    I have an ASP.net form on a website. I need to edit its actions based on the user selections, and make the radio button list allow multiple choices. I have the compiled web application, all passwords and access, all files from the original build (I b

  • is it possible to edit the arrow which we get in the select tag (Rails) 2010-10-21

    is it possible to edit the arrow which we get in the select tag (Rails) I want to replace the arrow which we get in the select tag(drop down element) with my own image.. Is it possible to do the same? --------------Solutions------------- It's not pos

  • Eclipse Plugin: Enablement of an Action based on the current selection 2010-04-24

    I am using the org.eclipse.ui.popupMenus extension point for adding a sub-menu whose Action that is bounded to the following class: public class MyAction implements IObjectActionDelegate { private Logic logic = Logic.getInstance(); // Singleton publi

  • jquery multiple select x rails edit action 2010-04-27

    i'm using the JQUERY multiselectable plugin. Because it's get one select box and transform it in two, where you can move one selected option to another select box. The problem is that i'm facing some problems with the edit action. When i load the for

  • trigger a 'change' action when a dropdown is 'selected' not when the dropdown list values are changed 2011-06-16

    I've got a few dropdown lists which are created dynamically. I've done a decent job of keeping my code DRY, but I have a small problem. I instruct jQuery to trigger an event when a select list 'changes', and seeing as the list are built dynamically,

  • How to apply the action to several Outlook items selected? 2011-06-17

    The code here works for one item only. How to modify that to run DisplayItemMetadata (see the code) for each item selected? Upd. tried to do the following: For Each individualItem In Application.ActiveExplorer.Selection With objButton .BeginGroup = T

  • Performing an action when clicking on a select option, but with unknown id 2014-02-26

    i'm facing a big problem with jquery, wordpress and twig here. My aim is to display a predefined select when the user selects an option from a select. The problem is that i don't know a way to check which option he has selected, because i can't get t

  • jquery javascript: take action when radio button is selected 2009-11-07

    I have the following radio buttons, none of which is checked by default. <input type="radio" name="location" value="0" /> home <input type="radio" name="location" value="1" /> work <input type="radio" name="location" value="2" /> sc

  • Fire a controller action from a jQuery Autocomplete selection 2011-04-13

    I have a jQuery autoselect box displaying the correct data. Now I would like to fire an ASP.NET MVC 3 controller when an item is selected. The controller should then redirect to a View. Here's my jQuery autocomplete code (I'm sure something is missin

  • Is it possible to create an action link for just one selection in the column of the grid view? 2011-07-15

    I'm writing an asp.net mvc "reservations" app. in c# where in a grid view I have a few statuses of the review process, like "new", "pending, "approved", "declined" along with details about each request. Is it possible for just one review status ("Pen

  • Using collection action in Active admin for selected objects 2011-11-05

    I am implementing an admin panel with Active Admin for my rails application. I have a comment section inside admin panel, and I'd like to approve a group of comments by checking the comments I'd like to approve and then clicking update button. In my

  • Dynamic Select Using Rails 3 and jQuery 2011-03-23

    I want to dynamically populate a dropdownlist using jQuery. I am trying to call a controllers action but I don't know how with the collection_select object. I have tried the :input_html but the controller action is never called. I receive a parsing e

  • Multilevel select on rails 2011-03-29

    I want to use multiple select drop-down between several models I have this: class Report < ActiveRecord::Base belongs_to :region end class City < ActiveRecord::Base has_many :regions end class Region < ActiveRecord::Base has_many :reports be

  • Why does destroy action trigger HTTP authentication in Production in Rails 3? 2011-04-20

    Screenshot of what happens when I press 'delete': http://twitpic.com/4mljuy This is what I see in my production.log: Started POST "/clients/1" for 127.0.0.1 at 2011-04-20 13:48:26 -0500 Processing by ClientsController#destroy as JSON Parameters: {"id

  • Drop down select ruby rails 2011-07-28

    Here's my code for a form that contains a drop down list - <div class="field"> <%= f.label :type, "Select profile type"%> <%= f.select :type, Profile::TYPES, :prompt => "Select a profile type" %> </div> The drop down menu l

  • Dynamic Collection Select in Rails using jQuery and AJAX 2011-08-04

    I have a simple application, in which I allow users to create posts. In order to create a post, users need to select a category. I want to use a collection select to show the category. When a user selects a category, I need to show another collection

  • How to implement multiple selection on Rails 3 2011-08-22

    I have an index where I'm showing a list of documents. I would like to implement a multiple select in order to do different actions to the documents the user has selected I have created a <%= check_box_tag 'id', 'document.id %> for each documen

  • How to send data to controller with remote select in Rail 3? 2011-12-12

    I want to do is load the price of a service. These services are listed in a select tag. <%= f.collection_select( :service_id, Service.all, :id, :name, :prompt => 'Select a service') %> <%= f.label :Price%> <%= f.number_field :price,

  • Getting radio button selection using Rails 2012-02-20

    I'm displaying a table of radio buttons, each of which corresponds to a recipe with a unique id. I'm using a submit tag to submit exactly which radio button was pressed, indicating which recipe was selected. I'm trying to pass the id to my controller

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.226 (s). 12 q(s)