on ruby on rails and forms

Discussion in 'Ruby' started by Andres Montano, Aug 8, 2005.

  1. Does anybody know if the options for a select in a form are accesible via
    @params?

    I have an application that succesfully builds the select html statements but
    then the user modifies the options via javascript. When the form gets
    posted, is there a way to know what options are there, which are selected,
    etc from the controller?

    I have tried all sorts of strategies like adding the [] to the name of the
    select tag, but however I name it, param always returns nil.

    Thanks,
    Andres
     
    Andres Montano, Aug 8, 2005
    #1
    1. Advertising

  2. Hi.

    I wonder if there's a rails list...

    Can you be more specific?

    So you've got your helper code down to create HTML select, that's
    cool... but what do you mean by "use modifies the options via
    javascript" - how?

    Julian.

    On 08/08/2005, at 12:01 PM, Andres Montano wrote:

    > Does anybody know if the options for a select in a form are
    > accesible via
    > @params?
    >
    > I have an application that succesfully builds the select html
    > statements but
    > then the user modifies the options via javascript. When the form gets
    > posted, is there a way to know what options are there, which are
    > selected,
    > etc from the controller?
    >
    > I have tried all sorts of strategies like adding the [] to the name
    > of the
    > select tag, but however I name it, param always returns nil.
    >
    > Thanks,
    > Andres
    >
    >
    >
    >
     
    Julian Leviston, Aug 8, 2005
    #2
    1. Advertising

  3. Thanks for the quick reply Julian. I will provide more detail here.

    I have two tables: roles and permissions with a "has_and_belongs_to_many"
    relationship between each other.

    In the edit action for role, I initialize role and permissions like this:

    @role = Role.find(@params[:id])
    @permissions = Permission.find :)all, :eek:rder => "title")

    In the model I have two lists: one with all the available permissions and
    another with the permissions that a specific role has and buttons that move
    stuff from one list to the other using javascript.

    The rhtml code for the lists goes like this:

    <select id="available_permissions" name="available_permissions" multiple
    size="10">
    <% available_permissions = @permissions - @role.permissions %>
    <%= options_for_select (available_permissions.collect { |p| [p.title,
    p.id]}) %>
    </select>

    <select id="role_permissions" name="role[permissions][]" multiple =
    "multiple" size="10">
    <%= options_from_collection_for_select(@role.permissions, "id", "title")
    %>
    </select>

    I have two buttons invoke a javascript method to remove an item from one
    list and add it to the other list. It goes like this:

    function moveSelected(orig, dest)
    {
    var selectOrig = document.getElementById(orig);
    var selectDest = document.getElementById(dest);
    var i = 0;
    while (i<selectOrig.length)
    {
    option = selectOrig.options;
    if (option.selected)
    {
    option.selected = false;
    selectOrig.remove(i);
    if (nn6)
    {
    selectDest.length = selectDest.length+1;
    selectDest.options[selectDest.length-1] = option;
    }
    else selectDest.add(option);
    }
    else i++;
    }
    }

    This part works well with records that I added manually to the
    permissions_roles intermediate table. Nevertheless the simple
    if @role.update_attributes(@params[:role]) ...

    is not intelligent enough to populate role.permissions and update the tables
    in the database. That is fine, as long as I can access the select options
    from the controller, but whatever name I put in params doesn't seem to work.
    The hash returned by @params[:role] or @params["role"] doesn't include
    "permissions" (i.e. @params[:role]["permissions"] is nil). Changing the name
    to something else like id = "assocperms" name = "assocperms[]" doesn't help
    as @params["assocperms"] also is nil (and so is @params["assocperms[]"],
    etc.). Also removing the "[]" from the name didn't do the trick. Is my only
    alternative to wrap up the select list in a div, send it via a
    form_remote_tag, and parse it manually? The must be a more elegant solution.

    If anyone can provide insight on how I can tackle this problem it will be
    greatly appreciated!

    Andres

    "Julian Leviston" <> wrote in message
    news:...
    > Hi.
    >
    > I wonder if there's a rails list...
    >
    > Can you be more specific?
    >
    > So you've got your helper code down to create HTML select, that's cool...
    > but what do you mean by "use modifies the options via javascript" - how?
    >
    > Julian.
    >
    > On 08/08/2005, at 12:01 PM, Andres Montano wrote:
    >
    >> Does anybody know if the options for a select in a form are accesible
    >> via
    >> @params?
    >>
    >> I have an application that succesfully builds the select html statements
    >> but
    >> then the user modifies the options via javascript. When the form gets
    >> posted, is there a way to know what options are there, which are
    >> selected,
    >> etc from the controller?
    >>
    >> I have tried all sorts of strategies like adding the [] to the name of
    >> the
    >> select tag, but however I name it, param always returns nil.
    >>
    >> Thanks,
    >> Andres
    >>
    >>
    >>
    >>

    >
    >
    >
     
    Andres Montano, Aug 8, 2005
    #3
  4. Andres Montano

    Brad Wilson Guest

    On 8/7/05, Julian Leviston <> wrote:
    > I wonder if there's a rails list...


    There is.

     
    Brad Wilson, Aug 8, 2005
    #4
  5. Isn't what you're trying to do AJAX?

    J

    On 08/08/2005, at 2:01 PM, Andres Montano wrote:

    > Thanks for the quick reply Julian. I will provide more detail here.
    >
    > I have two tables: roles and permissions with a
    > "has_and_belongs_to_many"
    > relationship between each other.
    >
    > In the edit action for role, I initialize role and permissions like
    > this:
    >
    > @role = Role.find(@params[:id])
    > @permissions = Permission.find :)all, :eek:rder => "title")
    >
    > In the model I have two lists: one with all the available
    > permissions and
    > another with the permissions that a specific role has and buttons
    > that move
    > stuff from one list to the other using javascript.
    >
    > The rhtml code for the lists goes like this:
    >
    > <select id="available_permissions" name="available_permissions"
    > multiple
    > size="10">
    > <% available_permissions = @permissions - @role.permissions %>
    > <%= options_for_select (available_permissions.collect { |p|
    > [p.title,
    > p.id]}) %>
    > </select>
    >
    > <select id="role_permissions" name="role[permissions][]" multiple =
    > "multiple" size="10">
    > <%= options_from_collection_for_select(@role.permissions, "id",
    > "title")
    > %>
    > </select>
    >
    > I have two buttons invoke a javascript method to remove an item
    > from one
    > list and add it to the other list. It goes like this:
    >
    > function moveSelected(orig, dest)
    > {
    > var selectOrig = document.getElementById(orig);
    > var selectDest = document.getElementById(dest);
    > var i = 0;
    > while (i<selectOrig.length)
    > {
    > option = selectOrig.options;
    > if (option.selected)
    > {
    > option.selected = false;
    > selectOrig.remove(i);
    > if (nn6)
    > {
    > selectDest.length = selectDest.length+1;
    > selectDest.options[selectDest.length-1] = option;
    > }
    > else selectDest.add(option);
    > }
    > else i++;
    > }
    > }
    >
    > This part works well with records that I added manually to the
    > permissions_roles intermediate table. Nevertheless the simple
    > if @role.update_attributes(@params[:role]) ...
    >
    > is not intelligent enough to populate role.permissions and update
    > the tables
    > in the database. That is fine, as long as I can access the select
    > options
    > from the controller, but whatever name I put in params doesn't seem
    > to work.
    > The hash returned by @params[:role] or @params["role"] doesn't include
    > "permissions" (i.e. @params[:role]["permissions"] is nil). Changing
    > the name
    > to something else like id = "assocperms" name = "assocperms[]"
    > doesn't help
    > as @params["assocperms"] also is nil (and so is @params["assocperms
    > []"],
    > etc.). Also removing the "[]" from the name didn't do the trick. Is
    > my only
    > alternative to wrap up the select list in a div, send it via a
    > form_remote_tag, and parse it manually? The must be a more elegant
    > solution.
    >
    > If anyone can provide insight on how I can tackle this problem it
    > will be
    > greatly appreciated!
    >
    > Andres
    >
    > "Julian Leviston" <> wrote in message
    > news:...
    >
    >> Hi.
    >>
    >> I wonder if there's a rails list...
    >>
    >> Can you be more specific?
    >>
    >> So you've got your helper code down to create HTML select, that's
    >> cool...
    >> but what do you mean by "use modifies the options via javascript"
    >> - how?
    >>
    >> Julian.
    >>
    >> On 08/08/2005, at 12:01 PM, Andres Montano wrote:
    >>
    >>
    >>> Does anybody know if the options for a select in a form are
    >>> accesible
    >>> via
    >>> @params?
    >>>
    >>> I have an application that succesfully builds the select html
    >>> statements
    >>> but
    >>> then the user modifies the options via javascript. When the form
    >>> gets
    >>> posted, is there a way to know what options are there, which are
    >>> selected,
    >>> etc from the controller?
    >>>
    >>> I have tried all sorts of strategies like adding the [] to the
    >>> name of
    >>> the
    >>> select tag, but however I name it, param always returns nil.
    >>>
    >>> Thanks,
    >>> Andres
    >>>
    >>>
    >>>
    >>>
    >>>

    >>
    >>
    >>
    >>

    >
    >
    >
    >
     
    Julian Leviston, Aug 8, 2005
    #5
  6. Andres,

    To understand what's happening, you're going to have to look at HTML
    forms, and the queries it builds. (Use GET method instead of POST so you
    can look at it.) Your error has nothing to do with Rails, except in that
    it can't read the user's mind. I'm way too tired to explain right now,
    so start Googling. :) The short answer, if you want to stick with the
    same interface, is you're going to have to use some javascript to build
    up some hidden fields.

    Whether you use AJAX to send the updated results back before the user
    even hits submit is up to you. But if you want to, the prototype library
    that comes with Rails should help.

    Devin

    Andres Montano wrote:

    >Thanks for the quick reply Julian. I will provide more detail here.
    >
    >I have two tables: roles and permissions with a "has_and_belongs_to_many"
    >relationship between each other.
    >
    >In the edit action for role, I initialize role and permissions like this:
    >
    > @role = Role.find(@params[:id])
    > @permissions = Permission.find :)all, :eek:rder => "title")
    >
    >In the model I have two lists: one with all the available permissions and
    >another with the permissions that a specific role has and buttons that move
    >stuff from one list to the other using javascript.
    >
    >The rhtml code for the lists goes like this:
    >
    ><select id="available_permissions" name="available_permissions" multiple
    >size="10">
    > <% available_permissions = @permissions - @role.permissions %>
    > <%= options_for_select (available_permissions.collect { |p| [p.title,
    >p.id]}) %>
    ></select>
    >
    ><select id="role_permissions" name="role[permissions][]" multiple =
    >"multiple" size="10">
    > <%= options_from_collection_for_select(@role.permissions, "id", "title")
    >%>
    ></select>
    >
    >I have two buttons invoke a javascript method to remove an item from one
    >list and add it to the other list. It goes like this:
    >
    >function moveSelected(orig, dest)
    >{
    > var selectOrig = document.getElementById(orig);
    > var selectDest = document.getElementById(dest);
    > var i = 0;
    > while (i<selectOrig.length)
    > {
    > option = selectOrig.options;
    > if (option.selected)
    > {
    > option.selected = false;
    > selectOrig.remove(i);
    > if (nn6)
    > {
    > selectDest.length = selectDest.length+1;
    > selectDest.options[selectDest.length-1] = option;
    > }
    > else selectDest.add(option);
    > }
    > else i++;
    > }
    >}
    >
    >This part works well with records that I added manually to the
    >permissions_roles intermediate table. Nevertheless the simple
    > if @role.update_attributes(@params[:role]) ...
    >
    >is not intelligent enough to populate role.permissions and update the tables
    >in the database. That is fine, as long as I can access the select options
    >from the controller, but whatever name I put in params doesn't seem to work.
    >The hash returned by @params[:role] or @params["role"] doesn't include
    >"permissions" (i.e. @params[:role]["permissions"] is nil). Changing the name
    >to something else like id = "assocperms" name = "assocperms[]" doesn't help
    >as @params["assocperms"] also is nil (and so is @params["assocperms[]"],
    >etc.). Also removing the "[]" from the name didn't do the trick. Is my only
    >alternative to wrap up the select list in a div, send it via a
    >form_remote_tag, and parse it manually? The must be a more elegant solution.
    >
    >If anyone can provide insight on how I can tackle this problem it will be
    >greatly appreciated!
    >
    > Andres
    >
    >"Julian Leviston" <> wrote in message
    >news:...
    >
    >
    >>Hi.
    >>
    >>I wonder if there's a rails list...
    >>
    >>Can you be more specific?
    >>
    >>So you've got your helper code down to create HTML select, that's cool...
    >>but what do you mean by "use modifies the options via javascript" - how?
    >>
    >>Julian.
    >>
    >>On 08/08/2005, at 12:01 PM, Andres Montano wrote:
    >>
    >>
    >>
    >>>Does anybody know if the options for a select in a form are accesible
    >>>via
    >>>@params?
    >>>
    >>>I have an application that succesfully builds the select html statements
    >>>but
    >>>then the user modifies the options via javascript. When the form gets
    >>>posted, is there a way to know what options are there, which are
    >>>selected,
    >>>etc from the controller?
    >>>
    >>>I have tried all sorts of strategies like adding the [] to the name of
    >>>the
    >>>select tag, but however I name it, param always returns nil.
    >>>
    >>>Thanks,
    >>> Andres
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>

    >>
    >>
    >>

    >
    >
    >
    >
    >
    >
     
    Devin Mullins, Aug 8, 2005
    #6
  7. Hi Julian et all,

    > Isn't what you're trying to do AJAX?


    That is what I meant with "Is my only alternative to wrap up the select list
    in a div, send it via a
    form_remote_tag, and parse it manually?"

    Nevertheless I have issues with that approach. As I see it the real benefit
    of AJAX is to update parts of the page through information sent to and
    received from the server without reloading the whole page. But the thing is,
    for this particular application, since I am only moving items from one list
    to the other I have all the information I need on the client side to update
    such parts of the page only with javascript. Put in other words, this
    application should only need the JA part of AJAX! ;)

    Sending the information as hidden fields such as Devin Mullins and Jim
    Herring are proposing sounds like a smoother solution! This comes with a
    very stupid javascript related question. Is there some page that people use
    to know (Netscape and IE's) javascript's API? While writing these small
    javascript functions I search some for a good site with the precise syntax
    but everything seems really incomplete and vague. This page is nice:

    http://www.w3schools.com/js/default.asp

    but stuff is still missing from there. For instance, where can I find which
    constructors does Hidden take?

    Andres

    > On 08/08/2005, at 2:01 PM, Andres Montano wrote:
    >
    >> Thanks for the quick reply Julian. I will provide more detail here.
    >>
    >> I have two tables: roles and permissions with a
    >> "has_and_belongs_to_many"
    >> relationship between each other.
    >>
    >> In the edit action for role, I initialize role and permissions like
    >> this:
    >>
    >> @role = Role.find(@params[:id])
    >> @permissions = Permission.find :)all, :eek:rder => "title")
    >>
    >> In the model I have two lists: one with all the available permissions
    >> and
    >> another with the permissions that a specific role has and buttons that
    >> move
    >> stuff from one list to the other using javascript.
    >>
    >> The rhtml code for the lists goes like this:
    >>
    >> <select id="available_permissions" name="available_permissions" multiple
    >> size="10">
    >> <% available_permissions = @permissions - @role.permissions %>
    >> <%= options_for_select (available_permissions.collect { |p| [p.title,
    >> p.id]}) %>
    >> </select>
    >>
    >> <select id="role_permissions" name="role[permissions][]" multiple =
    >> "multiple" size="10">
    >> <%= options_from_collection_for_select(@role.permissions, "id",
    >> "title")
    >> %>
    >> </select>
    >>
    >> I have two buttons invoke a javascript method to remove an item from one
    >> list and add it to the other list. It goes like this:
    >>
    >> function moveSelected(orig, dest)
    >> {
    >> var selectOrig = document.getElementById(orig);
    >> var selectDest = document.getElementById(dest);
    >> var i = 0;
    >> while (i<selectOrig.length)
    >> {
    >> option = selectOrig.options;
    >> if (option.selected)
    >> {
    >> option.selected = false;
    >> selectOrig.remove(i);
    >> if (nn6)
    >> {
    >> selectDest.length = selectDest.length+1;
    >> selectDest.options[selectDest.length-1] = option;
    >> }
    >> else selectDest.add(option);
    >> }
    >> else i++;
    >> }
    >> }
    >>
    >> This part works well with records that I added manually to the
    >> permissions_roles intermediate table. Nevertheless the simple
    >> if @role.update_attributes(@params[:role]) ...
    >>
    >> is not intelligent enough to populate role.permissions and update the
    >> tables
    >> in the database. That is fine, as long as I can access the select
    >> options
    >> from the controller, but whatever name I put in params doesn't seem to
    >> work.
    >> The hash returned by @params[:role] or @params["role"] doesn't include
    >> "permissions" (i.e. @params[:role]["permissions"] is nil). Changing the
    >> name
    >> to something else like id = "assocperms" name = "assocperms[]" doesn't
    >> help
    >> as @params["assocperms"] also is nil (and so is @params["assocperms []"],
    >> etc.). Also removing the "[]" from the name didn't do the trick. Is my
    >> only
    >> alternative to wrap up the select list in a div, send it via a
    >> form_remote_tag, and parse it manually? The must be a more elegant
    >> solution.
    >>
    >> If anyone can provide insight on how I can tackle this problem it will
    >> be
    >> greatly appreciated!
    >>
    >> Andres
    >>
    >> "Julian Leviston" <> wrote in message
    >> news:...
    >>
    >>> Hi.
    >>>
    >>> I wonder if there's a rails list...
    >>>
    >>> Can you be more specific?
    >>>
    >>> So you've got your helper code down to create HTML select, that's
    >>> cool...
    >>> but what do you mean by "use modifies the options via javascript" -
    >>> how?
    >>>
    >>> Julian.
    >>>
    >>> On 08/08/2005, at 12:01 PM, Andres Montano wrote:
    >>>
    >>>
    >>>> Does anybody know if the options for a select in a form are accesible
    >>>> via
    >>>> @params?
    >>>>
    >>>> I have an application that succesfully builds the select html
    >>>> statements
    >>>> but
    >>>> then the user modifies the options via javascript. When the form gets
    >>>> posted, is there a way to know what options are there, which are
    >>>> selected,
    >>>> etc from the controller?
    >>>>
    >>>> I have tried all sorts of strategies like adding the [] to the name
    >>>> of
    >>>> the
    >>>> select tag, but however I name it, param always returns nil.
    >>>>
    >>>> Thanks,
    >>>> Andres
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>
    >>>
    >>>

    >>
    >>
    >>
    >>

    >
    >
    >
     
    Andres Montano, Aug 8, 2005
    #7
  8. Andres Montano wrote:

    >Is there some page that people use
    >to know (Netscape and IE's) javascript's API? While writing these small
    >javascript functions I search some for a good site with the precise syntax
    >but everything seems really incomplete and vague.
    >

    An (extremely) oldie but goodie:
    http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/

    For anything since then, I can't help, but
    http://www.amazon.com/exec/obidos/ASIN/0596000480/ seems to be recommended.

    Does Hidden even have a public constructor?

    Devin
     
    Devin Mullins, Aug 9, 2005
    #8
  9. Nuke on Rails

    Does anyone knows if there is any Nuke-like package for Rails? I mean
    something like PhPNuke. I personally think that the script generated by
    Rails are fine for a starter point, but some of my friends would like it.

    Marcelo Paniagua L.

    --
    Este correo esta libre de virus!
     
    Marcelo Paniagua, Aug 11, 2005
    #9
  10. Re: Nuke on Rails

    On 8/10/05, Marcelo Paniagua <> wrote:
    > Does anyone knows if there is any Nuke-like package for Rails? I mean
    > something like PhPNuke. I personally think that the script generated by
    > Rails are fine for a starter point, but some of my friends would like it.
    >=20
    > Marcelo Paniagua L.


    Take a look at this one:
    http://typo.leetsoft.com/trac/

    >=20
    > --
    > Este correo esta libre de virus!


    :p

    --=20
    Gerardo Santana G=F3mez Garrido
    http://www.openbsd.org.mx/santana/
    "Entre los individuos, como entre las naciones, el respeto al derecho
    ajeno es la paz" -Don Benito Ju=E1rez
     
    Gerardo Santana Gómez Garrido, Aug 15, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    2
    Views:
    533
  2. Replies:
    6
    Views:
    183
    Michael Campbell
    Mar 17, 2005
  3. Josef 'Jupp' SCHUGT
    Replies:
    5
    Views:
    176
    Stephan Kämper
    Jan 24, 2006
  4. Replies:
    2
    Views:
    111
  5. Jason Wong
    Replies:
    0
    Views:
    169
    Jason Wong
    Nov 20, 2007
Loading...

Share This Page