SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax to show partial if

    Hello Folks,
    I have a simple model, object that has a 2 attributes: name and active and I need to have only 1 active object.
    In the form to add new objects I have a text-field for the name and a select for active (yes/no). The issue is that if the user selects yes I need to check if there is an other active object (and this is not a problem) and if there is I would like, before saving the new object and changing the active value of the former active object, ask a confirmation.

    I was thinking to use Ajax to show a partial to ask for confirmation. But here I'm lost, I tried several "experiments" but they don't work.

    Can someone suggest me the right way ???

    THANK YOU
    Alla prossima ...

  2. #2
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it unclear ??? or too difficult ???

    THANK YOU
    Alla prossima ...

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    Alla prossima ...

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wondering, wouldn't it be easier to check if there's another active object when generating the form, and putting the appropriate JS handler on the "active" checkbox?

  6. #6
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry but I don't understand what you mean.

    I don't have a checkbox but a select (this is not a issue)
    Anyway I want that the user can change the active object but if there is a object already active when he tries to add an active object the application notifies that with something like "object1 is active, do you want to change active object ???"

    Could you please explain more your idea ???

    THANK YOU
    Alla prossima ...

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd recommend you invest in this book:

    Ajax on Rails

    It will both give you the basics, and list classes, method and techniques that will aid you.

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    macsig, can you determine when displaying the form, if there's an active object? (before the user enters the name and the active fields).

    Then you can do something like this:

    Code:
    <&#37; form_for ... %>
    <%= f.text_field :name ... %>
    
    <% if there_is_an_active_object %>
    <%= f.checkbox :active, ... :onclick => "javascript here that asks the user if he want to change the active object" %>
    <% else %>
    <%= f.checkbox :active ... %> <!-- no javascript here: there is no other active object -->
    <% end %>
    <% end %>
    Is this clear?

  9. #9
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Is this clear?
    Yes, I can determine it, I mean I have just to check it out on the DB.

    With your solution, when they check (or select YES, since I use a select) can I show a partial or I can just show a confirmation window ???

    THANKS GUYS
    Alla prossima ...

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can show a partial too, by unhiding a div with prototype's Element.show('the-id-of-the-div').

    Code HTML4Strict:
    <div id="the-id-of-the-div" style="visibility:hidden">
    your partial here
    </div>

  11. #11
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks
    I'm gonna try it
    Alla prossima ...

  12. #12
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Fenrir to bother you once again but ( since I'm a Rails newbie ) I need an other explanation about the code you suggested me.


    Quote Originally Posted by Fenrir2 View Post
    <% if there_is_an_active_object %>


    In which way can I call a method without an object ???? I mean the body of there_is_an_active_object could be something like

    Object.find_by_active(true)


    but where have I to define it ??? and call it ???


    THANK YOU
    Alla prossima ...

  13. #13
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can put it in a helper. The helpers are in app/helpers/{the-controller-name}_helper.rb You define it like an ordinary procedure.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •