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 !!