2013-04-18 153 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调用整合到一个地方,并且可以使错误处理变得更加简单。