I have a problem with my delete action and link. Here is my link :
<%= link_to 'Supprimer la thématique', #theme, method: :delete, data: { confirm:'Êtes-vous sûr de vouloir supprimer la thématique' }, :class => "btn btn-default" %>
And I don't know why, but this link redirect to the show action of my model 'theme'.
I can resolve this by using button_to but with a button_to I'm not able to set a data confirm :
<%= button_to "Supprimer la thématique", #theme, :method=>:delete, data: { confirm: 'Êtes-vous sûr de vouloir supprimer la thématique ?'}, :class=> 'btn btn-default' %>
Do you have any ideas why the link to doesn't redirect to the delete action or why I can't have a data confirm for my button_to ?
Clicking a link always sends a GET request, unless there's Javascript in place that does something different, such as sending an Ajax request or submitting a hidden form. When using regular resourceful routes, the show action and destroy action share a route, and determine what to do based on the method (as you probably know).
The button_to method should support a confirmation dialog; if it doesn't work, then there's probably some Javascript missing.
Both your problems point to a missing unobtrusive Javascript driver. If using jquery-rails (the standard option), make sure that it's listed in your Gemfile:
gem 'jquery-rails'
and that you're loading it in your application.js:
//= require jquery
//= require jquery_ujs
Also be sure that you're calling the javascript_include_tag method in your application layout. Once you've got jquery-rails loaded properly, you should be able to get the results you're looking for with either method you've attempted.
For more information on loading jquery-rails, see the Github repo.
Related
I am currently banging my head against the desk trying to figure out why this is not working. I am trying to get the ujs confirmation box to show up when a delete link is clicked. Currently, the item is deleted with no confirmation box. Here is my delete link:
<%= link_to "void", project, method: :delete, data: {comfirm: "Are you sure you want to delete this project?"} %>
Here is where it gets strange. The following link (not a delete link) works as expected:
<%= link_to "About", about_path, data: {confirm: "test test"} %>
I did some digging in the gem itself and was able to discover that, with the delete link, the data-message attribute is not being parsed correctly in the following code. Specifically, the
if (!message) { return true; }
is returning true, where message is defined as follows:
message = element.data('confirm')
Note: element is the entire link itself. Can anyone help me find out why this is happening? I am using Rails 3.2 if it helps.
I have done this, just make it confirm instead of comfirm, and it works fine.
<%= link_to "void", project,method: :delete, data: {confirm: "Are you sure you want to delete this project?"} %>
I'm relatively new to Rails, and have been programming a few months.
I'm trying to use the t() method for internationalization, but it doesn't seem to work when I ask for confirmation in a link_to.
For example, when I write
<%= link_to t( ".delete_student_info"),
#student,
method: :delete,
confirm: "child_deletion_confirmation"
%>
...I predictably get a link_to that works and asks the confirmation question
However, when I write
<%= link_to t( ".delete_student_info"),
#student,
method: :delete,
confirm: t( ".child_deletion_confirmation")
%>
...I get the following output
Child Deletion Confirmation" data-method="delete" href="/en/student_profiles/41" rel="nofollow">Delete Student Info
Is there something conceptual that I am missing? I've looked in the Rails Guides Rails i18n API, but it doesn't address this issue. I'm thinking that maybe the confirm: is something different, but I don't know how to look it up. Any ideas?
I tried this out on my Rails 4 console and it worked fine:
helper.link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: I18n.t("date.formats.default") }
# => "<a data-confirm=\"%Y-%m-%d\" href=\"http://www.rubyonrails.org/\">Visit Other Site</a>"`.
Now, note the behavior when using nil for the :confirm is like what you're seeing:
helper.link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: nil }
# => "Visit Other Site"
So this makes me think that somehow your translation is evaluating to nil. However, I can't seem to figure out how to duplicate that issue...
I'll expand this answer to try to help more if you can show what the translations file looks like?
I'm using rails3-jquery-autocomplete gem to try to complete a form. However, I'm just getting no response whatsoever.
Alright, here it goes:
In my view:
<%= form_tag chats_path do %>
<%= autocomplete_field_tag 'name', '', chats_autocomplete_chat_name_path %>
<% end %>
In my ChatsController:
autocomplete :chat, :name
In my routes:
get "chats/autocomplete_chat_name"
And finally in my application.js:
//= require autocomplete-rails
Although I'm sure the javascript is at least loading, since I put an alert to let me know that autocomplete-rails.js was loading. Any ideas?
Try using chrome -> tools -> developer tools and open up the "networking" tab. Then try again and see if you're form is being sent and if so, what the response is.
I'm not sure if there's a name for this particular kind of UI pattern, but I'd like to create a form that looks like:
Company Name: _____
...
Employees:
Name: _____ Title: _____
Name: _____ Title: _____
Add New Employee
Save Company
I'm creating or editing a company, but embedded in the form are an arbitrary number of "sub-forms" for that company's employees. If I click the Add New Employee button, a new employee sub-form should appear immediately before the button.
I've been away from Rails for a while and I'm still trying to get the hang of how things are done in Rails 3. In the old days, I would've done something like:
button_to "Add Employee", :remote => true, :action => :new_employee_form
and then created new_employee_form.js.erb, which would contain JavaScript that appends the new sub-form to the list of company employees.
But Rails 3 seems to have changed a lot of the plumbing that makes this work and this solution doesn't work at all.
What's the recommended way to implement this pattern in Rails 3?
Make sure you require ujs javascript files in application.js, and that you include application.js in your layout.
//= require jquery
//= require jquery_ujs
//= require jquery-ui
EDIT
Check the resulting html, it should be similar to:
<%= button_to "Create", :action => "create", :remote => true %>
# => "<form method="post" action="/images/create" class="button_to" data-remote="true">
# <div><input value="Create" type="submit" /></div>
# </form>"
method post, class button_to, data-remote true.
Check out these rails casts episodes:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
I use this extensively in my rails apps.
Remote_true must be set in the html_options part of the button_to parameters: http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to . So the following snipped should work:
button_to "Add Employee", {:action => :new_employee_form} , { :remote => true }
Edit:
Ok, as you just stated, the problem is, that the new form does not get submitted via Ajax. The reason for this is, that you need to attach event handlers to dynamic elements using the ".on" function: http://api.jquery.com/on/
I have two links:
<%= link_to("Edit", edit_product_path(product.id)) %>
<%= link_to("Delete", product, :method => :delete) %>
The generated links are:
Edit
Delete
When clicking both on Edit and on Delete, the GET method is used.
How Rails decided which method to use ?
What does data-method="delete" and rel="nofollow" mean in the Delete link ?
Browsers usually support GET and POST HTTP methods. To emulate PUT and DELETE verbs, Rails injects a special _method parameter when a form is submitted.
You specify the method you want to use by passing the :method option, as you did.
<%= link_to("Action with DELETE", path_to_something, :method => :delete) %>
<%= link_to("Action with PUT", path_to_something, :method => :put) %>
Unless specified, the default value is GET.
Starting from Rails 3, Rails uses unobtrusive JavaScript to handle the DELETE method. It passes the HTTP verb in the data-method attribute, which is an HTML 5 feature.
In your case, it doesn't work because you probably forgot to include a JavaScript library (e.g. Prototype or jQuery) and the Rails adapter.
Make sure you are using either jQuery or Prototype, and you are including the rails.js javascript file.
Also, don't forget to add the csrf_meta_tag.
<%= csrf_meta_tag %>
If you want to learn move, I wrote an article about Unobtrusive JavaScript in Rails 3 a few months ago.
Interesting. I can confirm that link_to with the ":method => :delete" option does work in the default scaffold. However, in trying to build a project without scaffolding, I cannot get link_to to work. button_to works with no problem, using the same parameters that failed with link_to.
In my HEAD tag I have the required javascript includes:
javascript_include_tag :defaults
csrf_meta_tag
Very confusing. I couldn't figure out what "secret sauce" scaffolding puts into play to make link_to work. While Rob's assertion that link_to delete does not work in Rails 3, may not be technically accurate, I found it practically accurate. Thanks Rob, I had been stuck for hours until I saw your post.
You can't use a link for a delete in rails 3
You need to use a button_to eg.
<%= button_to("Delete", product, :method => :delete) %>