0

it is me(again), but now I am with the problem that i´ve created an destroy action and when I want to delete an post and click in the link called "Delete" the browser display the 'show' page, like if the "Read post" link that i made was the same as the "Delete" link. I looked at the terminal when i click in the "Delete" and it shows as an GET method instead of an DELETE method. I´ve changed the 'link_to' to an 'button_to' and it works, but it does not show the message. I need help to proceed with the tutorials that I am watching to learn rails, so please help me :)

I am using Rails version 5.1.4, and I´ve tried everything that is on this website and nothing works (like installing the jquery gem and so on...)

All the codes are bellow:

Posts Controller :

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all.order("created_at DESC")
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to @post
    else
        render 'new'
    end
  end

  def show
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render 'edit'
    end
  end

  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

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

    def post_params
        params.require(:post).permit(:title, :body)
    end
end

Index Page :

<% @posts.each do |post| %>
    <h2><%= post.title %></h2>
    <div>
        <%= truncate(post.body, :lenght => 35) %>
    </div>
    <br>
    <%= link_to "Read Post", post %> 
    <%= link_to "Delete", post, method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>

New :

<h1>New Post</h1>

<%= render 'form' %>

Edit :

<h1>Edit Post</h1>

<%= render 'form' %>

Show:

<h1><%= @post.title %></h1>

<div>
    <%= @post.body %>
</div>
<br>
<%= link_to "Edit Post", edit_post_path(@post) %>

_Form:

<%= form_for @post do |f| %>
    <%= f.label :title %> <br>
    <%= f.text_field :title %><br>
    <br>
    <%= f.label :body %><br>
    <%= f.text_area :body %><br>
    <br>
    <%= f.submit %>
<% end %>

Routes:

Rails.application.routes.draw do
  resources :posts
  root 'posts#index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

Rake Routes command at the terminal :

 Prefix Verb   URI Pattern               Controller#Action
    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
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy
     root GET    /                         posts#index

And the terminal output when I click at "Delete" link :

Started GET "/posts/1" for 127.0.0.1 at 2017-10-29 14:33:17 -0200
Processing by PostsController#show as HTML
  Parameters: {"id"=>"1"}
  Post Load (1.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT
 ?  [["id", 1], ["LIMIT", 1]]
  Rendering posts/show.html.erb within layouts/application
  Rendered posts/show.html.erb within layouts/application (2.0ms)
Completed 200 OK in 239ms (Views: 166.0ms | ActiveRecord: 2.0ms)

PLEASE HELP ME !!

Gustavo Pires
  • 31
  • 1
  • 5
  • can you show is the content of your `application.js` file ? – Cyzanfar Oct 29 '17 at 16:45
  • Possible duplicate of [Rails 4 link\_to Destroy not working in Getting Started tutorial](https://stackoverflow.com/questions/18154916/rails-4-link-to-destroy-not-working-in-getting-started-tutorial) – jvillian Oct 29 '17 at 16:58

2 Answers2

0

This might be because you are missing jquery and jquery_ujs libraries included in your application.js file.

in app/views/layouts/application.html.erb add this:

<%= javascript_include_tag 'application' %>

And in your app/assets/javascripts/application.js

//# This file is compiled into one js file
//= require jquery
//= require jquery_ujs
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81
0

First make sure you have the libs loaded. I assume it is application.js file. You can also check the network tab in chrome dev tools to see if the file is loaded.

//= require jquery
//= require jquery_ujs

I am not sure, while it may be unlikely, can you try changing to this:

<% @posts.each do |post| %>
    <h2><%= post.title %></h2>
    <div>
        <%= truncate(post.body, :lenght => 35) %>
    </div>
    <br>
    <%= link_to "Read Post", post %> 
    <%= link_to "Delete", post_path(post), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>

The only thing I changed was link_to 'Delete', post_path(path)....

Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71