0

私はRuby on Railsに非常に慣れていません。mysql dbからレコードを追加、更新、および削除する小さなプロジェクトを作成しました

ruby アプリケーションから mysql db にレコードを正常に追加および削除できます

しかし、問題は既存のレコードを更新しようとしたときだけです

私のコードは次のとおりです。

コントローラ:

class BookController < 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
   def create
      @book = Book.new(params[:book])
      if @book.save
            redirect_to :action => 'list'
      else
            @subjects = Subject.find(:all)
            render :action => 'new'
      end
   end
   def edit
      @book = Book.find(:all)

      @subjects = Subject.find(:all)
   end
   def update
      @book = Book.find(params[:id])
      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

リスト HTML:

<% if @books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>
<ul id="books">
<% @books.each do |c| %>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>

<b><%= link_to "edit",  {:action => 'edit', :id => c.id} %></b>

<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this item?" %></b>
</li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new Book", {:action => 'new' }%></p>


Edit HTML:
=========
<h1>Edit Book Detail</h1>
<%= form_tag(:action=> "update") do%>

<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,
                         @subjects, :id, :name) %></p>
<p><label for="book_description">Description</label><br/>
<%= text_area 'book', 'description' %></p>
<%= submit_tag "Save changes" %>
<%end %>
<%= link_to 'Back', {:action => 'list' } %>

URL からレコードを編集しようとすると、次の例外が発生しますhttp://localhost:3000/book/edit/5

Showing C:/app/app/views/book/edit.html where line #5 raised:

undefined method `title' for #<Array:0x33315c0>
Extracted source (around line #5):

2: <%= form_tag(:action=> "update") do%>
3: 
4: <p><label for="book_title">Title</label>:
5: <%= text_field 'book', 'title' %></p>
6: <p><label for="book_price">Price</label>:
7: <%= text_field 'book', 'price' %></p>
8: <p><label for="book_subject">Subject</label>:

ところで、私はrails3、ruby1.2、およびmysql5.5を使用しています。

私は学習曲線の中にいるので、誰かがこの問題で私を助けることができれば非常に役に立ちます.

4

1 に答える 1

0

editメソッドの通常の意図が書籍レコードの 1 つを編集することである場合、何らかの理由ですべての書籍レコードをロードしています。

これを修正するには、before_filterレコードの読み込みを処理するフックを定義する必要があります。

class BookController < ApplicationController
  # Set a handler for loading the book for most actions, except those
  # where loading a single book is not relevant.
  before_filter :load_book, :except => [ :index, :new, :create ]

  def edit
    @subjects = Subject.find(:all)
  end

  def update
    # Call the update_attributes method that will throw an exception if
    # an error occurs.
    @book.update_attributes!(params[:book])

    redirect_to :action => 'show', :id => @book

  rescue ActiveRecord::RecordInvalid
    # This exception is triggered if there was an error saving the record
    # because of a validation problem.

    # Trigger 'edit' action
    edit
    # Render as if on 'edit' page
    render :action => 'edit'
  end

protected
  def load_book
    @book = Book.find(params[:id])
  end
end

find(:all)注意として、モデルでまたはを呼び出すときはいつでも、allすべてのシステム メモリを使い果たし、アプリケーションとそれが実行されているサーバーの両方をクラッシュさせるリスクがあります。レコード数が少ないと確信できる場合を除き、ページネーションは絶対に不可欠です。

を使用するbefore_filterと、さまざまな冗長なfind呼び出しを 1 か所に統合​​するのがはるかに簡単になり、エラー処理がはるかに簡単になります。

于 2013-04-18T15:06:10.753 に答える