SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: calling multiple rjs files
-
Feb 25, 2008, 03:15 #1
- 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" ?
THANKSAlla prossima ...
-
Feb 25, 2008, 10:45 #2
- 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
-
Feb 25, 2008, 12:00 #3
- Join Date
- May 2007
- Posts
- 121
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks
could you please provide to me a sample ofspecify 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 ...
-
Feb 25, 2008, 12:58 #4
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can easily find it yourself on http://railsmanual.com/
-
Feb 25, 2008, 15:38 #5
- 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 ...
-
Feb 26, 2008, 01:24 #6
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 26, 2008, 02:17 #7
- 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 ...
-
Feb 26, 2008, 07:07 #8
- 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
-
Feb 28, 2008, 20:44 #9
- 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 ...
-
Feb 29, 2008, 08:42 #10
- 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.
-
Feb 29, 2008, 11:53 #11
- Join Date
- May 2007
- Posts
- 121
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 29, 2008, 12:20 #12
- 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.
-
Feb 29, 2008, 17:00 #13
- 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 againAlla prossima ...
-
Mar 1, 2008, 06:49 #14
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In the model. Google "exceptions in ruby".
Bookmarks