Can I customize the delete warning in ActiveAdmin - ruby-on-rails-3

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).

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.

url_for adding controller and action to querystring in rails 3.2

I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.

Rails Devise not Deleting Account

I am starting a new web app and I am using Devise for the authentication.
I have got Devise working with register, login, edit and viewing the user profile. The only problem I am having is clicking the 'Delete Account' just redirects to /users and doesn't actually delete the user. What am I doing wrong?
The delete account button
= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete
As far as I know, I haven't changed anything that would break this. The delete account button I want to use is
= negative_trash_button_link_to "Cancel my account", registration_path(resource_name), :method => :delete, :confirm => "You sure?"
The negative_trash_button_link_to is in the css3buttons gem.
Edit: My routes.rb
resources :posts
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
root :to => "home#index"
devise_for :users do
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
resources :users, :only => :show
It would be helpful to tell us the rails version and the javascript libraries / frameworks that you have installed.
If you are using jquery or any other framework is working, test if it is working in general if is is using the appropriate helpers (for example the jquery-rails helpers in case of jquery). Most probably is a javascript issue, check also for any javascript errors.
Look your javascript console. You seems to have a javascript error which prevent the request to be sent.
Do you have the alert "Are you sure?" before the redirection ?

Simulate PUT on a link, in Rails3

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.

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