Rails - dynamically adding to sortable_element - ruby-on-rails

Am adding to a sortable list using Ajax, and to get the Scriptaculous effects to kick in after the add, the only way I have found is by re-executing sortable_element.
Can anyone suggest a better way of doing this, at the complete code is a hack:
><%= link_to_remote "Add",
:url => { :controller => "pages", :action => "add_fragment", :pid => pid, :index => index },
:complete => "eval(decodeURIComponent(#{sortable_element 'frag_list', :url => sort_frag_pages_path, :complete => visual_effect(:highlight, 'frag_list'), :handle => 'handle'}).gsub('//',''));" %>

the answer is to add this into the controller method called by the link_to_remote
page.sortable :frag_list

Related

Rails / Haml: How to create a post form?

I'm trying to make a simple form, but it's working not so fine.
This is my current form code:
%form{ :controller => 'tool', :action => 'activation', :method => 'post' }
%table{ :border => 0, :width => "100%", :height => "100%" }
%tr{ :align => "center", :valign => "center" }
%td
%input{ :type => "text", :name => "accountName" }
%input{ :type => "submit", :name => "submit", :value => "login" }
I am getting this url when trying to send data via form: 10.0.0.2:3000/activation.
I know that I can make route tool#activation to activation, but it's a wrong way, I want to sent post query to 10.0.0.2:3000/tool/activation, but :action => 'tool/activation' also is a bad way as far as I understand.
Can you give me advice ?
You should use the rails helper tags.
= form_tag tool_activation_path, :method => :post do
# The table
# The row
# The data
= text_field_tag "accountName", ""
= submit_tag "Submit"
See more here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
Also, you should try to avoid unnecessary tables to style your layout. Instead, look to using CSS.

Ruby on Rails: link_to_remote and rel

I want a link to remote to have a rel tag, because I want to use facebox with it.
I had it working with a regular link to... but I needed the link to remote to handle the event that a user doesn't have javascript enabled.
this, currently does't work (except for the non-javascript part )
<%= link_to_remote "Ask a Question",
{:url =>
{:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}, :rel => 'facebox'},
:href => url_for(
:controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true) %>
In link_to_remote you pass in HTML options (like rel) in the third argument. In your code you're passing it in the second (i.e. the first hash). Try this instead:
<%= link_to_remote("Ask a Question",
{ :url => { :action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}
},
{ :href => url_for( :controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true ),
:rel => 'facebox'
}
)
%>
(As you know, some of the parentheses and curly braces are optional, but here I've included all of them for clarity, and probably would keep them in since you're passing a lot of complex arguments here.)

Question regarding RJS and usage of link_to_remote

I was working on my website when I cam across this problem, I have 100 posts in my blog and I am paginating them 10 at a time, I display the 1st 15 and display a link at the bottom which basically is implemented using the link_to_remote tag.
<%= link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'} %>
I click on that and get the next 15 posts, and append it to the container containing the 1st 15 posts through insert_html
page.insert_html :bottom, :puzzles , :partial => 'puzzles', :object => #puzzles
Now what I want is the link at the bottom of the page to update too, so that the next time the user clicks more the 3rd batch is fetched and so on. Basically something like
page.replace_html 'more-link', link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'}
Any clues as to how I can do it?
You're very close.
replace_html is called with (DOM_id, options for render). Essentially you want to render the output of a link_to_remote call. But you're not passing it in a form that render can use. As Barry Hess points out replace is much better suited for this task, because most of what you're changing is the tag attributes.
Using replace_html would result in nested tags which is could cause problems. You want to replace the element entirely. Replace has the same syntax of replace_html so you would run into the same problems if you were just switch replace_html to replace.
Here's what you want:
page.replace 'more-link', :text => link_to_remote "More Posts",
:url => {:action => 'view' ,:id => link.to_i + 1} ,
:html => {:id => 'more-link'}
However I'm not sure if link_to_remote is accessible from RJS. If the above doesn't work you could always do this:
page.replace 'more-link', :inline => "<%= link_to_remote 'More Posts',
:url => {:action => 'view' ,:id => link.to_i + 1},
:html => {:id => 'more-link'} %>", :locals => {:link => link}

Field accessing incorrect controller action on onChange

I have a website and before i included the design and some changes(layout, scripts, etc), it was working fine. Then, I don't know what happened but the onChange method from my select field is accessing "show" action instead of the one that's written in the line.
I have the following at my New view:
<%= collection_select('category', 'id', #categories, 'id', 'name', {:prompt => "Choose a category"}, {:onchange => "#{remote_function(:url => {:controller => 'announcements', :action => "update_subcategories"}, :with => "'parent_id='+value")}",:class => 'newAd_box2_inputField'}) %>
The idea is that it updates a second select field with records related to the selected one.
The second select looks like this:
<%= render :partial => 'category_select', :object => #subcategories %>
Again, it was working great before I introduced some changes, but now it just won't go to "update_subcategories" action, it just goes to "show".
In my routes.rb I've got the following:
map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show'
map.new "/announcements/new", :controller => 'announcements', :action => 'new'
Does anybody know what's going on?
This is your problem.:
map.show "/announcements/:permalink", :controller => 'announcements',
:action => 'show'
Rails will use the first route to appear in routes.rb that matches the parameters given. This behaviour kicks in both when generating urls for output with url_for (which is what the :url option for remote_function does in the background) and mapping urls received by the server to actions.
The above route will generate the route /announcments/update_subcategories so the link will appear right when you view source the source in your browser. But when you go to that link, Rails will match it to the show action of the announcements controller.
The fix depends on which method you're using to define routes for the announcements controller. Regardless of which fix you use, it must come before the bad route. (map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show')
If you're using restful routes the fix is to add a :collection option to the definition.
map.resources :announcements, :collection => {:update_subcategories => :get}
If you're not using restful routes the fix is to add a route.
map.connect "/announcements/update_subcategories",
:controller => "announcements", :action => "update_subcategories"

Rails link_to_remote degradation and url_for

I am trying to create a link to destroy and entry in the DB using AJAX but I also want it to function without JavaScript enabled
However the following code
<%=link_to_remote "Delete", :update => "section_phone", :url => {:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id }, :href => url_for(:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id)%>
produces the output
Delete
For some reason the url_for does not work properly and the href tag is set to #.
I do not know why this does not work since I am not notified of any errors in the log
Does anyone know why this could be?
Thank you
You're missing curly braces around the options{} hash, which :update and :url belong to, to separate them from the html_options{} hash that :href belongs to.
Try this:
<%=link_to_remote "Delete", {:update => "section_phone", :url => {:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id }}, :href => url_for(:controller => "phone_numbers", :action => "destroy", :id => phone_number_display.id)%>
That'll get the URL to show up as the href attribute of your link, but a GET request to your destroy action shouldn't delete it. You'll need something else (like what vrish88 suggests) so that you can make a GET request to the destroy action to get a form, then POST that form to actually delete the phone number.
I believe your looking for something like this: RailsCasts - Destroy Without JavaScript

Resources