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
Related
It's common in Rails apps to have the Destroy action come along with a warning - "Are you sure you want to delete this?" The typical code looks like this:
link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
Is there a way in ActiveAdmin to customize the confirmation string for one model only (not globally)? I see that the string is loaded from the active_admin.delete_confirmation translation key. Can the string be model-specific?
Since ActiveAdmin 2.7 model-specific translations can be customized by placing them to active_admin.resources group in en.yml.
active_admin:
resources:
user:
delete_confirmation: Are you sure?
I don't think you can change the string with configuration.
However you can change the default actions:
Index table: https://github.com/activeadmin/active_admin/blob/master/docs/3-index-pages/index-as-table.md
index do
column :title
actions defaults: false do |post|
link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
end
end
Show page: https://github.com/activeadmin/active_admin/blob/master/docs/8-custom-actions.md#action-items
action_item only: :show do
link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
end
You can also overcome this ActiveAdmin limit via Javascript.
Here an example of app/assets/javascripts/active_admin.js:
//= require active_admin/base
$( document ).ready(function() {
$('body.admin_companies a[data-method=delete]').
data('confirm', 'New confirmation text per model Company.\n Ok?');
$('body.admin_users a[data-method=delete]').
data('confirm', 'New confirmation text per model User.\n Ok?');
});
where body.admin_companies is to limit this confirmation message to views of the model Company.
Similar for body.admin_users.
It works both on #index and #show.
The only downside is that it doesn't play well with i18n (if you need multilanguage avoid this solution).
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.
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 :)
So far, I know that in Rails I can simulate a PUT request using a form which has a hidden input with name='_method' and value='put', but I'm interested in simulating that for a link.
How can I have a link in a view that would fit this route:
match '/article/:id/publish', :to => 'article#publish', :via => :put
The docs for link_to say you can specify a :method option that creates a form that is submitted on clicking the link.
link_to "Publish!", publish_article_path(article), :method => :put
Not sure what your route helper method would be (I assumed publish_article_path - you should be able to figure it out with rake routes from the command line. The :method is the important part that will do the magic you want.
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).