my delete method is redirecting to the show method.

1,420 views
Skip to first unread message

John Pratt

unread,
Dec 7, 2016, 3:25:58 PM12/7/16
to RailsInstaller
I am doing a step by step web application from the Rails Guide provided by RailsInstaller.org, and I have so far completed
5.12 Using partials to clean up duplication in views. Although, when I got to the delete section of the lesson and completed all the things provided there, a problem took place in my blog application when I click the destroy action in  http://localhost:3000/articles,
 

Listing Articles

TitleText
Hi NickHow are things going?ShowEditDestroy
Doing fine.And you?ShowEditDestroy
Doing fineAs well.ShowEditDestroy
anything newhappen?ShowEdit

Destroy

 
Instead of executing the Destroy method, it will redirect me to the show method.
 
 
 
Title: Rails is Awesome

Text: It really is! 

Edit | Back
 
 
Is there any way to debug this problem?
My Rails version is Rails 4.2.5.1 and my Ruby version is ruby 2.1.8p440 (2015-12-16 revision 53160) [i386-mingw32]
Here are some of my .rb  .erb and  .js files. I was wondering if there are any flaws in my files that someone could call me out on.  OK..... Here they are...
 
 
app\controllers\articles_controller.rb
 
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end
 
  def show
    @article = Article.find(params[:id])
  end
 
  def new
    @article = Article.new
  end
 
  def edit
    @article = Article.find(params[:id])
  end
 
  def create
    @article = Article.new(article_params)
 
    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end
 
  def update
    @article = Article.find(params[:id])
 
    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end
 
  def destroy
    @article = Article.find(params[:id])
    @article.destroy
 
    redirect_to articles_path
  end
 
  private
    def article_params
      params.require(:article).permit(:title, :text)
    end
end
 
 
app\views\layouts\application.html.erb
 
<!DOCTYPE html>
<html>
<head>
  <title>Blog</title>
  <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
 
app\views\articles\show.html.erb
 
<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>
 
<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
 
 
 
app\views\articles\_form.html.erb
 
<%= form_for @article do |f| %>
 
  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @article.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
 
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>
 
  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>
 
  <p>
    <%= f.submit %>
  </p>
 
<% end %>                      
 
 
 
app\views\articles\index.html.erb
 
<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>
 
 
 
app\views\welcome\index.html.erb
 
<h1>Hello, Rails!</h1>
<%= link_to 'My Blog', controller: 'articles' %>
 
 
 
app\views\articles\new.html.erb
 
<h1>New article</h1>
 
<%= render 'form' %>
 
<%= link_to 'Back', articles_path %>
 
 
app\views\articles\edit.html.erb
 
<h1>Edit article</h1>
 
<%= render 'form' %>
 
<%= link_to 'Back', articles_path %>
 
 
 
 
app\models\article.rb

 

class Article < ActiveRecord::Base
  validates :title, presence: true,
                    length: { minimum: 5 }
end
 
 
 
config\routes.rb

 

Rails.application.routes.draw do
 
  resources :articles
 
  get 'welcome/index'
 
  root 'welcome#index'
 
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".
 
  # You can have the root of your site routed with "root"
  # root 'welcome#index'
 
  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'
 
  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
 
  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products
 
  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end
 
  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end
 
  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end
 
  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable
 
  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end
 
 
 
db\migrate\20161129162508_create_articles.rb
class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title
      t.text :text
      t.timestamps null: false
    end
  end
end
 
 
 
app\assets\javascripts\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 any plugin's vendor/assets/javascripts directory 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
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
 
 
I would greatly appreciate a response. 
 
John

John Pratt

unread,
Dec 27, 2016, 3:42:15 PM12/27/16
to RailsInstaller
Could someone give me a response already?¿?¿?¿?? It has been weeks and no response. SOMEONE, ANYONE. HELP!!!!!

Surya Avantsa

unread,
Dec 27, 2016, 9:40:08 PM12/27/16
to railsin...@googlegroups.com
I world look at the delete method in the controller and tweak it the way I want.


On Tue, Dec 27, 2016 at 12:42 PM John Pratt <johnath...@gmail.com> wrote:
Could someone give me a response already?¿?¿?¿?? It has been weeks and no response. SOMEONE, ANYONE.  HELP!!!!!



--

You received this message because you are subscribed to the Google Groups "RailsInstaller" group.

To unsubscribe from this group and stop receiving emails from it, send an email to railsinstalle...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Sent from my iPhone

Surya Avantsa

John Pratt

unread,
Jan 24, 2017, 11:05:26 AM1/24/17
to RailsInstaller
But how? the only temporary solution i could find was to change the link_to method in my index to the button_to method, and even then it wont execute the confirm deletion. 


Original Index

<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>


The Revised Index 

<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= button_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>

    </tr>
  <% end %>
</table>

Bryan Bibat

unread,
Jan 24, 2017, 4:16:22 PM1/24/17
to railsin...@googlegroups.com
Rails uses Javascript to emulate HTTP DELETE (browsers usually support only GET and POST). My guess is that either there is a JS error on your page preventing Rails from converting the link's behavior to DELETE. Open your browser's Dev tools (Ctrl-Shift-I on chrome) and see if there is a JS error.

Another possibility is that the JS isn't loaded on your page. You can view the page's source in the browser to check if there's a script tag pointing to application-____.js. Also, double check if you've disabled Javascript in your browser.

-bry

To unsubscribe from this group and stop receiving emails from it, send an email to railsinstaller+unsubscribe@googlegroups.com.

John Pratt

unread,
Jan 26, 2017, 12:11:43 PM1/26/17
to RailsInstaller
Yes there is a javascript error and a css error.

John Pratt

unread,
Jan 26, 2017, 2:16:00 PM1/26/17
to RailsInstaller
what to do next?

Bryan Bibat

unread,
Jan 26, 2017, 3:52:53 PM1/26/17
to railsin...@googlegroups.com
Well, what are the errors? I don't have psychic powers to see what's on your screen halfway across the world  lol

A list of the errors or a screenshot would help.

To unsubscribe from this group and stop receiving emails from it, send an email to railsinstaller+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages