2013-04-18 208 views
0

我很新蜂Ruby on Rails的執行編輯操作,我剛纔已經創建了一個小項目,添加,更新和刪除MySQL數據庫記錄無法在Ruby on Rails的

我能夠成功添加從MySQL數據庫從Ruby應用程序

刪除記錄,但問題是,只有當我嘗試更新現有記錄

我的代碼如下,

控制器:

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

BTW我使用Rails3中,ruby1.2和MYSQL5 0.5。

,因爲我在一個學習曲線,這將是非常有用的,如果有人能幫助我在這個問題上。

+0

你的意思是紅寶石1.9.2,而不是1.2吧? – tadman

+0

另外'找到(:所有)'贊成all'的'已經過時了。 – tadman

回答

0

出於某種原因,你加載所有的書籍記錄時edit方法的通常目的是編輯之一。

爲了解決這個問題,你應該定義一個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調用整合到一個地方,並且可以使錯誤處理變得更加簡單。