0

I am a beginner of Ruby on rails, I failed when I try to delete the object that I create. here is the code. controller

class PuzzlesController < ApplicationController
  def index
    @puzzles = Puzzle.all
  end
  def show
    @puzzle=Puzzle.find(params[:id])
  end
  def new
    @puzzle = Puzzle.new

  end
  def create
      #render plain: params[:puzzle].inspect
    @puzzle = Puzzle.new(puzzle_params)
    if(@puzzle.save)
      redirect_to @puzzle
    else
      render 'new'
    end
  end
  def edit
    @puzzle=Puzzle.find(params[:id])
  end
  def update
    @puzzle=Puzzle.find(params[:id])
    if(@puzzle.update(puzzle_params))
      redirect_to @puzzle
    else
      render 'edit'
    end
  end
  def destroy
    @puzzle = Puzzle.find(params[:id])
    @puzzle.destroy

    redirect_to puzzle_path
  end
  private def puzzle_params
    params.require(:puzzle).permit(:title,:body,:category,:difficulty)
  end
end

show

<h2><%= @puzzle.title %></h2>
    <p><%= @puzzle.body %></p>
    <p><%= @puzzle.category %></p>
    <p><%= @puzzle.difficulty %></p>
<hr>
<%= link_to "Edit", edit_puzzle_path(@puzzle), :class => 'btn btn-default'%>
<%= link_to "Delete", puzzle_path(@puzzle),
            method: :delete,
            data: {confrim: 'are you sure? '},
             :class => 'btn btn-danger'%>

when i click on delete, its like re-render the page. i searched a lot on internet and cant find a solution for it.

this is the information on rails server.

Started GET "/puzzles/1" for ::1 at 2017-04-02 18:51:18 +0930
Processing by PuzzlesController#show as HTML
  Parameters: {"id"=>"1"}
  Puzzle Load (0.5ms)  SELECT  "puzzles".* FROM "puzzles" WHERE "puzzles"."id" = ? LIM             IT ?  [["id", 1], ["LIMIT", 1]]
  Rendering puzzles/show.html.erb within layouts/application
  Rendered puzzles/show.html.erb within layouts/application (1.0ms)
Completed 200 OK in 63ms (Views: 46.2ms | ActiveRecord: 0.5ms)
yue
  • 1
  • 1

2 Answers2

0

change your redirect path

redirect_to puzzles_path
Kenoyer130
  • 6,874
  • 9
  • 51
  • 73
0

There are several issues in your code:

1) There is a typo in the link_to parameter list (confirm, instead of confrim):

<%= link_to 'Delete', puzzle_path(@puzzle),
      method: :delete,
      data: { confirm: 'are you sure? '},
      class: 'btn btn-danger' %>

2) At the end of your destroy method you cannot redirect to a singular puzzle path, because you just deleted the puzzle. Redirect to the index page instead:

redirect_to puzzles_path

3) But most important. Links with method: 'delete' only work with JavaScript and Rails 5.0 depends on jQuery. Because you wrote that the links just redirects to the show page, I guess you do not have included the Rails JavaScript files into your asset pipeline:

# Add this to the head of your `views/layout/application.rb`:
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

# Ensure that your `assets/javascripts/application.js` include the following lines:
//= require jquery
//= require jquery_ujs

Ensure in your browser's concole that the files are loaded without an error.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • thank for you help, I have fixed the mistakes on 1) and 2). 3) the rails javeScript have been included in application.js //= require jquery //= require jquery_ujs //= require turbolinks //= require_tree . at the head of "views/layout/application.html.erb", there is a line <%= javascript_include_tag 'default', 'data-turbolinks-track': 'reload' %>. Do i need to change the 'default' to 'application'? – yue Apr 02 '17 at 09:52
  • If your main JavaScript file is named `application` and not `default`, then you should change the `javascript_include_tag` to include the file named `application`. I am surprised that you didn't get an `404 (not found)` when trying to load a file named `default` that doesn't exist. – spickermann Apr 02 '17 at 10:02
  • a error appearance on that line . ExecJS::ProgramError in Puzzles#show. TypeError: The object does not support this property or method. – yue Apr 02 '17 at 10:10
  • 1
    i solved it, this problem is about using JScript on Windows. here is solution http://stackoverflow.com/questions/12520456/execjsruntimeerror-on-windows-trying-to-follow-rubytutorial – yue Apr 02 '17 at 11:11