2

I have this code:

<table>
 <% @posts.each do |post| %>
  <tr>
   <td><%= post.created_at.utc.strftime("%d.%m.%Y") %></td>
   <td><%= link_to(post.title, {:controller=>:blog,:action=>:show, :id=>post}) %></td>
   <td><%= link_to 'Edit', edit_post_path(post) %></td>
   <td><%= link_to 'Destroy', {:controller=>:posts,:action=>:destroy, :id=>post}, :method=>:delete %></td>
 </tr>
<% end %>

Problem is that link_to 'Destroy' isn't deleting posts, but just open them. Any suggestions to solve this?

routes.rb file:

LivuPamatskola::Application.routes.draw do
resources :posts
get "admin/rakstu_red"
get "home/par_skolu"
get "home/personals"
get "home/kontakti"
get "home/pers_pieeja"
get "home/galerijas"
get "home/index"
root :to => 'home#index'

posts.controller.rb destroy section:

def destroy
   @post = Post.find(params[:id])
   @post.destroy

   respond_to do |format|
     format.html { redirect_to posts_url }
     format.json { head :no_content }
  end
end

application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

layout head:

<head>
<title>Līvu pamatskola</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%= stylesheet_link_tag 'style' %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>

SOLVED:

Found solution on this post: Rails 3.1 link_to not showing confirmation or destroying properly

Anyway, thank you guys for helping!

Community
  • 1
  • 1
RydelHouse
  • 235
  • 1
  • 8
  • 19

1 Answers1

3
<%= link_to 'Destroy', post, :method => :delete, :confirm => "Are you sure?" %>

also you could clean a little edit link

<%= link_to post.title, post %>

And edit link

<%= link_to 'Edit', [:edit, post] %>
fl00r
  • 82,987
  • 33
  • 217
  • 237