我关注了这一集http://railscasts.com/episodes/136-jquery-ajax-revised 并创建了我的 cusotm 示例。我将创建表单放入索引中,并通过远程方法创建一本书
但我不知道如何在页面中放置错误消息。请举个例子,谢谢~
指数
<%= render 'form' %>
<p>
<table id='books_tbl' class="table">
<th>id</th>
<th>title</th>
<th>ISBN</th>
<th>sn</th>
<th>price</th>
<th>Functions</th>
<div class="books" id="books">
<%= render @existed_books %>
</div>
</table>
控制器
# POST /books
# POST /books.json
def create
@book = Book.new(params[:book])
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render json: @book, status: :created, location: @book }
format.js
else
format.html { render action: "new" }
format.json { render json: @book.errors, status: :unprocessable_entity }
format.js
end
创建.je.erb
<% unless @book.save %>
<% else %>
$('#books_tbl tr:last').after('<%= j render(@book) %>');
<% end %>