-2

I'm programming a website with a friend, and I'm getting one error with my code. In the computer of my friend, he have the same code and don't have any error.

I get the following error:

We're sorry, but something went wrong.

Well, we're newbie in Rails and I get this error when I try to load the posts page. We think the error is on deleting a post, because if we comment the line of "posts_delete_path", everything works. For delete a post, we are using:

show.hmtl.erb:

<% @hashtag.posts.each do |p| %>
    <li>Posted by: <%= p.user.name %></li>
    <li>Content:</li>
    <li><%= p.content %></li>
    <li><a href="<%= posts_delete_path %>/<%= p.id %>" >Delete</a></li>
<% end %>

routes:

   match '/signout', to: 'sessions#destroy'
   match 'posts/delete/:id', to: 'posts#destroy'
   resources :posts, :only => [:create, :show]

posts controller:

def destroy
    @post = Post.find(params[:id])
    if current_user.id == @post.user_id
       @post.destroy
    end
end

---- edit ---- rake routes:

      posts GET    /posts(.:format)           posts#index
            POST   /posts(.:format)           posts#create
   new_post GET    /posts/new(.:format)       posts#new
  edit_post GET    /posts/:id/edit(.:format)  posts#edit
       post GET    /posts/:id(.:format)       posts#show
            PUT    /posts/:id(.:format)       posts#update
            DELETE /posts/:id(.:format)       posts#destroy

We know that's the newbie way to delete a post, but we don't find any better solution. (If you want suggest something, we will be glad too).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Paladini
  • 4,522
  • 15
  • 53
  • 96
  • Well, I don't know, just can't delete the posts, instead delete the posts, I see the posts. (instead "delete" method, I get "post" method) – Paladini Mar 27 '13 at 13:30

4 Answers4

0

You should use the link_to notation like this:

<%= link_to "Delete", p, method: :delete %>

Also, you should try to stop users from even seeing the delete button in the view, something like:

<% if current_user == p.user %>
    <%= link_to "Delete", p, method: :delete %>
<% end %>

This will only show the delete link only to the owner of the post.

Also like Ganesh said , turn this:

match 'posts/delete/:id', to: 'posts#destroy'
resources :posts, :only => [:create, :show]

to:

resources :posts, :only => [:create, :show, :destroy]

Checkout the routing system in the rails guides: http://guides.rubyonrails.org/routing.html, and see which of the routes you will need.

Zippie
  • 6,018
  • 6
  • 31
  • 46
  • Thanks Zippie, but when I do the modifications that you told to me, when I click in the link "Delete", I'm redirected to "http://localhost:3000/posts/12". Can help? – Paladini Mar 27 '13 at 12:32
  • ahhh, try this: resources `:posts, :only => [:create, :show, :destroy]` or just make the `resources :posts` – Zippie Mar 27 '13 at 12:38
  • Don't work again :/ I search for "link_to rails" on Google and I open the APIDOC of this. Look what I found: "Supported verbs are :post, :delete and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. " What's JavaScript they're talking? Because the described error is the same I have here. – Paladini Mar 27 '13 at 12:44
  • check this out: http://stackoverflow.com/questions/4446697/why-rails-link-to-does-not-work-for-delete-action – Zippie Mar 27 '13 at 13:08
  • Sure, and thanks for trying to help me. I edited the question, see above the routes. – Paladini Mar 27 '13 at 13:12
  • Thanks @Zippie, but I can't destroy posts yet. The rails.js of this link works for Coffee Script? (sorry, I really don't know about Javascript / jQuery / Coffee) – Paladini Mar 27 '13 at 13:57
  • Now is working, I changed link_to to button_to and it's works! Thanks for everyone. – Paladini Mar 28 '13 at 13:04
  • cool, glad it worked and that you solved the problem on your own. – Zippie Mar 29 '13 at 10:11
0
<%= link_to "Delete", p, method: :delete %>

and modify the routes.rb with

resources :posts

Ganesh Kunwar
  • 2,643
  • 2
  • 20
  • 36
0

looks like ur link is fine, but sounds like you did not include your js, thats why it is redirecting you to the show page when you click delete.

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



1. Check your /assets/application.js

for //= require jquery and //= require jquery_ujs



Add in application.js this code

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

2. Check your /layouts/application.html.erb

for <%= javascript_include_tag "application"%>

<head>
    <title>My awesome app</title>
    <%= stylesheet_link_tag "application" %>
    <%= javascript_include_tag "application"%>
    <%= csrf_meta_tag %>
  </head>
Francois
  • 10,465
  • 4
  • 53
  • 64
  • Thanks for help. I don't understand the first step Francois, can you explain? Just copy and paste this code into my project or need change something? When I do by the first way, I get "We're sorry, but something went wrong." Below the code of the first step I have: "window.<# ProjectName #> = { ... } window.fbAsyncInit = function() { ... } – Paladini Mar 27 '13 at 13:28
  • I solved the problem, just pasted your code below my code. But I still can't delete the posts, I'm redirected to "show", instead use the "delete" method, use the "post"(or "get", I don't know) method. Do you know what's problem? – Paladini Mar 27 '13 at 13:34
  • Just a little thing, I don't know anything about jQuery / JavaScript, but for what I know, we use Coffee in our project. This changes something? @Francois – Paladini Mar 27 '13 at 13:44
  • try removing the `window.Instablah` and `window.fbAsyncInit` so the file starts with `// This is a manifest fil` and try again see what happends? – Francois Mar 27 '13 at 13:46
  • Well, any part of my project work, because need be logged in to do things. Just a thing, I don't need the "rails.js" in my project? – Paladini Mar 27 '13 at 13:51
  • Are you running rails3? Check your layouts if you have `<%= javascript_include_tag "application "%>` in the head – Francois Mar 27 '13 at 13:57
  • Now is working, I changed link_to to button_to and it's works! Thanks for everyone. – Paladini Mar 28 '13 at 13:00
  • Hi @francois, you can see the answer above (it's my own answer) or here: <%= button_to "Delete", { :controller => :posts, :action => 'destroy', :id => p.id }, :method => :delete %> – Paladini Mar 29 '13 at 01:32
0

Thanks for everyone who tried help me. Now I solved my problem, but by other way.

What I do is:

In view:

<% if current_user.id == p.user_id %>
            <%= button_to "Delete", { :controller => :posts, :action => 'destroy', :id => p.id }, :method => :delete %>
<% end %>

In controller:

  def destroy
    @post = Post.find(params[:id])
    #if current_user.id == @post.user_id
       @post.destroy
    end
  end

In routes:

resources :posts

Thanks guys!

Paladini
  • 4,522
  • 15
  • 53
  • 96