After learning Ruby-on-Rails and "killing users" doesn't work

I recently installed Ruby on Rails 3.2 and tried to learn it. I have followed along with the RoR 3.0 tutorial (http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#top) and so far everything is going well (yes I know there is a 3.2 version).

I am currently stuck with section 10.4.2, which describes how to add a link to kill users. He says to add code

<%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
                            :title => "Delete #{user.name}" %>

      

Besides adding to apps / view / layout / application / html / erb

<%= javascript_include_tag :defaults %>

      

It looks like it should use the destroy method correctly in the custom controller as the tutorial says, but it doesn't work for me and I can't figure out why. The link it creates is just / user /: id. I've looked at the same section in tutorial 3.2 and it's pretty much the same direction (but doesn't include the javascript include tag code). I cannot get it to work after this tutorial. So I'm not sure why it isn't working or how to get it to work.

So we get it, instead of going to the destroy method in this custom controller, it goes to / user /: id, which is the show method.

+1


source to share


3 answers


Deleting a resource (user in your case) requires the jquery_ujs javascript file to be included in the page. Usually the action "show" is encountered because without jquery_ujs does not send hidden data that points to an HTTP DELETE verb.

Try to explicitly insert jquery_ujs like below:

<%= javascript_include_tag 'jquery_ujs' %>

      

and see what happens.



jquery_ujs is intended to be ... an unobtrusive scripting support file for Ruby on Rails, but not tied to any particular backend. '. In other words, it scans the document, sees the special data- * attributes, and performs various actions depending on those attributes, such as adding hidden html elements, performing ajax requests, etc.

Also note that in order to use jquery_ujs in jquery, (before) must also be specified.

Hope it helps.

+5


source


My problem was that I was not referencing jquery. The addition has been //=jquery

fixed.



+1


source


Hi, you can also try this:

application.html.erb:

<%= javascript_include_tag 'jquery_ujs' %>

OR

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %>

      

and your link code should look like this:

<%= link_to "<i class='icon-trash'></i> Delete".html_safe, user, :confirm => "Are you sure you want to delete this user? " + user.name + "?" ,:method => :delete%>

      

and your controller should have the following:

def destroy
 @item = Item.find(params[:id])
 if @item.destroy
  redirect_to users_path, :notice => "Successfully deleted a user."
 else 
  redirect_to users_path, :notice => "Failed to delete a user."
 end
end

      

0


source







All Articles