Render Link_to_remote via ajax in rails - ruby-on-rails

I am working in rails 2. I want to make an link_to_remote object and render it on html page. I want to send date via ajax. I am trying this.
render :text => "<font color='"+ color +"'>" +params[:ajax_status] + "</font>" + <%= link_to_remote '| Undo',
:update => 'status_'" +params[:ajax_status]+ ",:url => {:controller => 'requests', :action => 'action_on_punching_request', :id => "+ params[:id] +" :ajax_status => 'Undo'} %>
But unable to proceed. It shows on webpage
Accepted<%= link_to_remote '| Undo', :update => 'status_'Accepted,:url => {:controller => 'requests', :action => 'action_on_punching_request', :id => 20 :ajax_status => 'Undo'} %>
PLease help me out.

You don't need the <%= %> tags, in this case - they're for erb.
Try just:
render :text => "#{ content_tag :font, params[:ajax_status], :color => color }#{ link_to_remote '| Undo', :update => "status_#{ params[:ajax_status] }", :url => {:controller => 'requests', :action => 'action_on_punching_request', :id => params[:id], :ajax_status => 'Undo'} }"

Related

How to send link_to parameters using POST on Rails 2

I have a link_to like this:
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, :class => "new_link" %>
There's a way to send those parameters not using the query string for it? (using post instead of a get) ??
Thank you!
I already tried:
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, {:method => :post,:class => "new_link"} %>
And it keeps doing the same thing...!
Add a supported HTTP verb in the method option.
<%= link_to "link", {:method => :post ...} %>
I think you can just add :method => :post to the options of link_to.
Here are the docs (for Rails 3, but I think this still holds true for Rails 2 also) http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
EDIT FOR UPDATED QUESTION
:method => :post belongs in the HTML options, not the URL options. This is not very obvious from the documentation.
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, {:method => :post, :class => "new_link"} %>

Rails Routing Error

In routes.rb I have:
get "survey/show" => "survey#show"
post "survey/step_2" => "survey#step_2"
post "survey/step_3" => "survey#step_3"
And in step_2.html.erb I have:
<%= form_for #result, :url => { :controller => 'survey', :action => 'step_3' } do |f| %>
And in survey_controller.rb I have:
def step_2
#result = Result.new(params[:result])
if #result.save
session[:result_id] = #result.id
render :action => "step_2"
else
render :action => "show"
end
end
def step_3
#result = Result.find(session[:result_id])
if #result.update_attributes(params[:result])
render :action => "step_3"
else
render :action => "step_2"
end
end
And when I submit the form on step_2 I get the following error:
No route matches "/survey/step_3"
I believe Rails form_for method may be making that a PUT request, since the #result object has an id. I believe you should change your form_for line to:
<%= form_for #result,
:url => { :controller => 'survey', :action => 'step_3' },
:html => { :method => :post} do |f| %>
or change the route type to put in routes.rb
You have to use match.
match 'survey/step_3' => 'survey#step_3', :via => 'post'
I might be wrong about the :via, but it's something like that.

Obeserve_field on ruby on rails

I want a search field to start outputting results once a user statrs typing
Here is what I got so far
<%= observe_field 'keyword', :frequency => 0.5,
:update => 'results',
:loading => "Element.show('spinner')",
:complete => "Element.hide('spinner')",
:url => { :action=> 'search_results' } %>
then in my controller this is what i got. The text field is within a form i dont know if that makes a difference.
def search_results
keyword = params[:keyword]
#tutors = Tutors.find(:all,:conditions => ["category LIKE ?", '%' + keyword + '%'])
end
If I understand your question correctly your action is not receiving the keyword as expected. Is this right? If so then it's because you need to add a :with argument to your observe_field call. Like this:
<%= observe_field 'keyword',
:frequency => 0.5,
:update => 'results',
:loading => "Element.show('spinner')",
:complete => "Element.hide('spinner')",
:url => { :action=> 'search_results' },
:with => 'keyword' %>

Sortable_element with RJS does not working

I have a list of images where user can arrange their orders.
When user uploaded an image, I want the list to still be sortable.
I am using a similar upload that was described here: http://kpumuk.info/ruby-on-rails/in-place-file-upload-with-ruby-on-rails/
Please help.
Here are the code for upload in view file:
<% form_for [:admin, #new_image], :html => { :target => 'upload_frame', :multipart => true } do |f| %>
<%= hidden_field_tag :update, 'product_images'%>
<%= f.hidden_field :image_owner_id %>
<%= f.hidden_field :image_owner_type %>
<%= f.file_field :image_file %><br />
or get image from this URL: <%= f.text_field :image_file_url %>
<%= f.hidden_field :image_file_temp %><br />
<%= f.submit "Upload Image" %>
<% end %>
And in controller view:
def create
#image = Image.new(params[:image])
logger.debug "params are #{params.inspect}"
if #image.save
logger.debug "initiating javascript now"
responds_to_parent do
render :update do |page|
logger.debug "javascript test #{sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)}"
page << "show_notification('Image Uploaded');"
page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)
end
end
#render :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
else
responds_to_parent do
render :update do |page|
page << "show_notification('Image Upload Error');"
end
end
end
end
Or, to rephrase the question:
Running this:
page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)
Will NOT adding sortable list feature.
Please help,
Thank you
I think the problem may well be that the sortable is created when the page first loads. I have run into this issue a couple of times. Adding a new element to the list means you have to call the sortable function again to have the new element included in the sorting.
I am not across prototype/scriptaculous, but there may be a way to have the sortable element listen for events that add elements to the list. jQuery has a set of rebinding events that will respond to new elements added to the DOM, might be something simialr in the other libs.
Found it!
Instead of:
page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
Use
page.replace params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}

Highlighting the link_to_remote link

Hi i have the rails application. when i call the home controller i have index action . in the index.html.erb .i have some link_to_remote links.
<li><%=link_to_remote "Example",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html =>{:id =>"cb"},
:with => "'choose=' +encodeURIComponent('value')" %></li>
<li><%=link_to_remote "Test",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html => { :id =>'cb1'},
:with => "'choose=' +encodeURIComponent('value')" %></li>
After clicking the "Example" and "Test" option the respective div got updated .... i want to highlighting the options after user click happened... consider If "Example" clicked i want to highlight "Example" option background...
I tried with current_page? rails helper method, :complete attribute of link_to_remote but no luck can any one please suggest me on this. ... Thanks in advance
there are params[:controller] and params[:action] that help you to know which action is current.
<div id='example_link'>
<%= link_to_remote "Example",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html =>{:id =>"cb"},
:with => "'choose=' +encodeURIComponent('value')" %>
</div>
in your home controller
def bank
# your code
render :update do |page|
page << "$('#example_link').addClass('highlighted')"
end
end
in css
.highlighted{
background-color: yellow;
}

Resources