How to use :onclick and :confirm together in link_to Rails 3? - ruby-on-rails-3

I am trying to hide a div on click, after confirmation from the user.
I have something like this:
<%= link_to 'X', user, :method => :delete, :remote => true, :class => 'delete_link', :onclick => "jQuery('#user_#{user.id}').hide();" %>
Whenever the 'x' is clicked, I get a confirmation popup, but onclick event is not executed.
I want to hide the div whenever user gives Yes in the popup. How do I do this in rails 3 way?

You can hide immediately after click this way.
$('.delete-item[data-remote]')
.data('type', 'html')
.on('ajax:before', function(event, data) {
$(this).closest('.item').remove();
});
If you want to wait until the response is back, replace 'ajax:before' with 'ajax:success'

You can use the other way, rather rails 3 way. You can hide your div by having destroy.js.erb. and write you div hidding Jquery code there, #user_#{user.id}').hide();
Hope, this will help you :)

Related

How to add html link for a simple_form field in rails 3.2?

Here is the quote_task field in simple form.
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => #quote_task.id}, :readonly => true %>
Now we want to add an embeded html link for the #quote_task.id to show the content of the quote_task. When a user click the id, he will be directed to the content of the quote task. Something like:
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => (link_to #quote_task.id.to_s, quote_task_path(#quote_task.id))}, :readonly => true %>
Is there a way to do this in simple_form? Thanks for help.
your expectation for HTML are way beyond any possible semantic.
http://www.w3schools.com/tags/tag_input.asp
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element
Yes it is possible that when one click on a input, it will show desired content. However without JS this wont be possible
input:
<%= f.input :quote_task, :label => t('Quote Task'), :readonly => true, class="redirecter", :'data-quote-task-path'=>quote_task_path(#quote_task.id) %>
coffee script using jQuery:
#app/assets/javascript/my_input.coffee
jQuery ->
$('input.redirecter').click ->
path = $(this).data('quote-task-path')
document.write(path); # ugly redirect, use Ajax
simple solution, but better would be if you load some content from server to your page with Ajax http://api.jquery.com/jQuery.ajax/
but my opinion is that you shouldn't do this at all. Inputs are for forms, forms are for submitting data. What you should be really using is pure link_to without any input due to HTML semantics. If you want it to look like input that you can style it to look like input, point is don't rape input tag for what it not meant to do.
it's not possible to embed anchors within input fields.
you can use javascript to do whatever magic that field should have.
if you want to find out more about javascript. go to amazon, buy a book, read it.

Display 'show' action in partial within the 'index' action's view - rails

I have a property search page (:controller => 'properties', :action => 'index') that consists of a right sidebar that has a search form which displays the search results below the form. When the user clicks on a property in the right sidebar I want to display the details of that property in the main area of the index page on the left.
Right sidebar is a partial called properties/_property.html.erb:
<%= form_tag properties_path, :method => 'get' do %>
<%= text_field_tag 'location', (params[:location]) %>
<%= select_tag(:max, options_for_select([['No Max', ""], ['$100,000', 100000], ['$200,000', 200000], etc %>
more search fields for baths beds etc
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #properties.each do |property| %>
<%= link_to([property.Address,property.City].join(", "), {:action => 'show', :id => property.id}) %>
<li><strong><%= number_to_currency(property.Price, :precision => 0) %></strong></li>
etc etc
<% end %>
The only way I know how to show the property details is with the 'show' action, but that takes the user to a new page, for example localhost:3000/properties/1865. I've made the 'show' view with the same layout as the 'index' view and have made the right sidebar a partial (properties/_property.html.erb) which appears on both 'show' and 'index' so when the user clicks on a property in the right sidebar and goes to localhost:3000/properties/1865 the property details are displayed correctly in the main area and the right sidebar is on the right.
But because localhost:3000/properties/1865 is a different page than localhost:3000/properties/index the search form in the right sidebar has forgotten it's parameters which means the list of search results in the right sidebar has changed back to the default list of all properties.
How can I display the 'show' action within a partial on the index page so the user's search parameters are remembered by the form in the right sidebar? Or if I have to go to the 'show' page how can I make the right sidebar stay exactly as it is?
Any ideas greatly appreciated, just a suggestion in the right direction would be good, have spent all day trying to figure it out and have got nowhere, thanks
I have made the show view with the same layout as the index view and
have made the right sidebar a partial (properties/_property.html.erb)
which appears on both show and index.
Yes but this will only get you a similar layout for both the pages. You want the list to persist between different requests.
You basically have two options. use session to remember the list which I wont recommend.
Other is you use form :remote => true or ajax and update the page partially.
EDIT:
What version of rails you are using? Do u have jquery loaded in your application?
Follow this SO POST.
You might have to change your show action a bit.
respond_to do |format|
format.js {render :partial => 'property_details' ,:layout => false}
format.html
end
Create a partial for property details and just put body content here.
And link will look like
<%= link_to "link name", {:action => :show, :id => item_id}, :remote => true ,:html => {:class => 'links_product'} %>
Also to update the view you may use(make sure you have rails.js in your page):
$(document).ready(
function(){
$("a.links_product").bind("ajax:success",
function(evt, data, status, xhr){
$("#response").html(data); // in case data is html. (_*.html.erb)
}).bind("ajax:error", function(evt, xhr, status, error){
console.log('server error' + error );
});
});
Have a div with id response or any valid id. Done!

Rails simple_forms collections radio button broken down

I am attempting to make a voting system for some video. On the vote page I want a radio button for each finalist and next to it an image pulled form youtube the file name and the finalist name. I have a domain for submission and user and made a domain for the voting that is associate to both with the fields submission_id and user_id with submission has_many votes and users has_one vote. Using simple form I create the following:
= simple_form_for #vote do |f|
= f.input :submission_id, :as => :radio, :collection => #finalists, :label_method => :file_name, :value_method => :id, :label => false
= f.submit "Submit Vote 2"
This does work and it puts up a radio button for each and allows the user to select on of the submissions to vote for. However, wanted to put next to the radio button a thumbnail and some text to make it clear what they are voting for. I tried just making a field of HTMl text that of the all the information I wanted:
<img src="youtube/thumbnail" /> <div> Text for label</div>
However, it just prints out the HTML mark up. tried raw(:file_name) but that errored out with bad syntax.
At this point I though I might just need to break down the colleciton and make the loop my self. I tryed the following:
= simple_form_for #vote do |f|
- #finalists.each do |finalist|
= f.input :submission_id, :as => :radio, :value => finalist.id, :input_html => {:name => 'vote[submisison_id]', :id => "vote_submission_id_#{finalist.id}", :value => finalist.id}
= f.submit "Submit Vote"
This results in 5 different radio button collections with their values being yes/no. I of course want one set of radio buttons where one of the submissions gets selected.
I have looked up a few different page on radio_button_tag and collections and I am having a lot of troubles trying to figure out how to tell rails that I want all the radio button to be part of the same set.
Any help would be much appreciated. Thank you.

help with a rails ajax button_to :remote

Hi Im trying to make a button_to :remote with Rails3, which when onclick it will fetech some content and display success alert when ajax did a round trip back with status 200.
my code now =
<%= button_to 'helloworld', '/ccs', :remote => true ,:method=>'get', :disable_with => 'loading', :confirm => "are u sure?", :success =>"alert('lol');" , :failure=>"alert('omg');"%>
It dose send another HTTP request when button clicked but just not having any action on success or failure.
What's wrong with it anyone?
Rails 3 no longer has support for prototype helpers and their callbacks, such as :success and :failure.
Read more on this page, particularly sections 3 and 4:
http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
As you can see, you'll have to bind those callbacks manually (the page above uses jQuery), but you won't be able to do so inline.
Alternatively, button_to_remote, which will do exactly what you want, is now available as a plugin:
http://github.com/rails/prototype_legacy_helper

What's the alternative of :before & :after of rails 2.3.* in Rails 3?

I am using remote form in Rails 3. It works fine but I want to show / hide spinner during ajax request.
In rails 2.3.* we use :before & :after in remote form to show/hide spinner
What should I do in Rails 3, as remote form of Rails 3 doesn't contain such options.
Here is a working solution I tried:
In my view file, I use :onSubmit to show a spinner:
<% form_for("", #search,
:url => {:action => "search"},
:html => {:id => "search_form",
:onSubmit => "$('search-loader').show();"},
:remote => true) do |f| %>
In my search action, I added one line to hide it:
render :update do |page|
...
page << "$('search-loader').hide();"
end
It works great..!
Well, I'm using jQuery, and I'm doing the following, trying to be unobtrusive:
Add this, right before your </head> tag:
= yield :document_ready
Then in your application_helper.rb:
def document_ready(content)
html = %{ $(function(){#{content}})}
content_for(:document_ready){ javascript_tag(html) }
end
This allows you to load and run javascript once your document is loaded.
On top of the view containing your form add:
- document_ready("hide_button_show_spinner('your_button_id')")
In application.js
function hide_button_show_spinner(element_id) {
$('#'+element_id).bind('click', function() {
$('#'+element_id).after("<img src='/path/to/your/spinner.gif' class='ajax_loader' id='"+element_id+"_ajax_loader' style='display: none'/>")
$('#'+element_id).hide();
$('#'+element_id+'_ajax_loader').show();
});
}
This will hide the button and show the spinner once the button is clicked. You may need to adapt this to your specific case.
You can then show the button and hide the spinner in your javascript response (the .js.erb file that you render from the action called by the ajax request).