BooksController#update中的ActiveRecord :: RecordNotFound

时间:2014-03-19 05:40:01

标签: ruby-on-rails-4

我是ruby以及rails的新手我在点击更新按钮时遇到以下错误

ActiveRecord::RecordNotFound in BooksController#update
                Couldn't find Book without an ID
                Extracted source (around line #35):
                def update
                  @book = Book.find(book1_params[:id])........here it is line number 35
                  if @book.update_attributes(params[:book])
                    redirect_to :action => 'show', :id => @book

                activerecord (4.0.3) lib/active_record/relation/finder_methods.rb:266:in `find_with_ids'
                activerecord (4.0.3) lib/active_record/relation/finder_methods.rb:35:in `find'
                activerecord-deprecated_finders (1.0.3) lib/active_record/deprecated_finders/relation.rb:122:in `find'
                c:in `find'
                app/controllers/books_controller.rb:35:in `update'

books_controller.rb

我在更新方法中遇到上述错误。

class BooksController < ApplicationController
      def list
        @books = Book.find(:all)
      end
      def show
        @book = Book.find(params[:id])
      end
      def new
        @book = Book.new
        @subjects = Subject.find(:all)
      end
      #  private 
      def book_params
        params.require(:book).permit(:title,:price,:subject,:description)
      end
      def create
        @book = Book.new(book_params)
        if @book.save
          redirect_to :action => 'list'
        else
          @subjects = Subject.find(:all)
          render :action => 'new'
        end
      end
      def edit
        @book = Book.find(params[:id])
        @subjects = Subject.find(:all)
      end

      def book1_params
        params.require(:book).permit(:id)
      end

      def update
        #below is the line I am getting the error....
        @book = Book.find(book1_params)

        if @book.update_attributes(params[:book])
          redirect_to :action => 'show', :id => @book
        else
          @subjects = Subject.find(:all)
          render :action => 'edit'
        end
      end
      def delete
        Book.find(params[:id]).destroy
        redirect_to :action => 'list'
      end
      def show_subjects
        @subject = Subject.find(params[:id])
      end
    end

的routes.rb

LibrarayWebApplication::Application.routes.draw do

    get 'books/list'
    get 'books/new'
    post 'books/create'
    post 'books/update'
    get 'books/list'
    get 'books/show'
    get 'books/edit'
    get 'books/delete'
    get 'books/update'
    get 'books/show_subjects'
  end

查看

以下代码从数据库获取数据并显示用户。如果用户需要他/她可以更新信息,但是当我按下更新按钮时它显示错误。 我使用的是rails4和eclipse IDE,以及mysql2 gem和MySQL数据库。我能够执行创建,删除,列表或显示操作,但我无法执行更新操作。我很无奈,可以提出建议,通过完整的应用程序了解rails4的详细信息

<h1>Edit Book Detail</h1>
<%=form_tag :action => 'update', :id => @book %>    
  <p>
    <label for="book_title">Title</label>:    
    <%= text_field 'book', 'title' %>
  </p>

  <p>
    <label for="book_price">Price</label>:
    <%= text_field 'book', 'price' %>
  </p>

  <p>
    <label for="book_subject">Subject</label>:        
    <%= collection_select(:book, :subject_id, enter code here @subjects, :id, :name) %>      
  </p>

  <p>
    <label for="book_description">Description</label><br/>
    <%= text_area 'book', 'description' %>
  </p>

  # the below line is to update the content in the database          
  <%= submit_tag "Save changes" %>
  <%= link_to 'Back', {:action => 'list'} %>

1 个答案:

答案 0 :(得分:0)

替换

def update
        #below is the line I am getting the error....
        @book = Book.find(book1_params)

        if @book.update_attributes(params[:book])
          redirect_to :action => 'show', :id => @book
        else
          @subjects = Subject.find(:all)
          render :action => 'edit'
        end
      end

def update
  #below is the line I am getting the error....
  @book = Book.find(params[:id])

  if @book.update_attributes(book_params)
    redirect_to @book
  else
    @subjects = Subject.all
    render 'edit'
  end
end