我非常喜欢ruby on rails,我刚刚创建了一个小项目,它添加,更新和删除mysql db中的记录
我能够从ruby应用程序
成功添加和删除mysql数据库中的记录但问题仅在于我尝试更新现有记录
我的代码如下,
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
<% 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' } %>
当我尝试编辑来自网址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.5。
由于我处于学习曲线中,如果有人可以在这个问题上帮助我,那将非常有用。
答案 0 :(得分: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
调用合并到一个位置,并且可以简化错误处理。