SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

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

    calling multiple rjs files

    Hello, I have just added AJAX to show a notice in my view and it works fine showing a static message but I need to show a message according to the number of element I have in my DB. For instance if the table is empty the message shall be "message1", however if there is at least 1 element the message shall be "message2" and this works fine. But I would like to have different styles for each message so I though to call a different rjs for each condition but I don't know how.

    How can I specify the name of rjs file to call?

    What do I have to use instead "format.js" ?

    THANKS
    Alla prossima ...

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can specify the rjs to call in a parameter for the link/form helpers. You can also use ifs in rjs:

    Code ruby:
    # rjs file
    if something
      show message 1
    else
      show message 2
    end

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks
    could you please provide to me a sample of
    specify the rjs to call in a parameter for the link/form helpers
    .

    I guess I'm going to use the if within the rjs file but, you know, just to improve my rails skills.

    Thanks again and have a nice day!
    Alla prossima ...

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can easily find it yourself on http://railsmanual.com/

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I guess I got it


    link_to_remote "Delete this post",
    url => { :action => "destroy", :id => post.id },
    :update => { :success => "posts", :failure => "error" }
    Alla prossima ...

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

  7. #7
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually I did in a different way

    Code Ruby:
     if count == 0
           @category = Category.find(params[:id])
           @category.destroy
     
           flash[:notice] = lc(:destroyed)
           respond_to do |format|
             format.html { redirect_to categories_path }
             format.js   # Execute destroy.rjs
           end
         else
            flash[:notice] = lc(:not_destroyed)
            respond_to do |format|
              format.html { redirect_to categories_path }
              format.js { render :action => 'no_destroy.rjs' }
            end
          end
    Alla prossima ...

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively you could override destroy() to check if the category is empty and if it isn't it throws an exception.

    You can then code the controller like this:

    Code ruby:
    def the_action
      Category.find(params[:id]).destroy
      flash[:notice] = lc(:destroyed)
      respond_to do |format|
        format.html { redirect_to categories_path }
        format.js   # Execute destroy.rjs
      end
    rescue
      flash[:notice] = lc(:not_destroyed)
      respond_to do |format|
        format.html { redirect_to categories_path }
        format.js { render :action => 'no_destroy.rjs' }
      end
    end

  9. #9
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, could you please explain to me the vantages to use this approach instead mine.

    Moreover, which is the right way to call rjs file in Rails 2: if I call destroy.rjs it works fine, however if I call it destroy.js.rjs it doesn't but I would like to have the names with .extension.template.

    Have a nice day!
    Alla prossima ...

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It feels cleaner to me. If you're going to use it then it's better to use a different method name for destroying + exception if not empty.

    I just use .rjs, because rjs is always js. You should be able to use .js.rjs though.

  11. #11
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    it's better to use a different method name for destroying + exception if not empty.
    Thanks, I see your point so, if I understand well you suggest to check out in destroy if it is empty and after calling "the_action" but how can I do that? I mean I can I hook the exception?

    Thanks and have a nice day!
    Alla prossima ...

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you can "rescue" the exception like all other exceptions in Ruby. In fact that is what I did in the code I posted. You raise the exception in checked_destroy() in your model, and you rescue it in the controller action.

  13. #13
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    I'm sorry but that you call "checked_destroy" (that it is the same action you called earlier "the_action") should be in the controller or in the model?

    Could you please provide to me a sample. I really appreciate it.

    Thanks again
    Alla prossima ...

  14. #14
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the model. Google "exceptions in ruby".


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
  •