新手刚看完 tutorials,试做一个 Tasks 功能的页面,在使用 f.submit 时,设计按钮应链接到 update 方法,但实际却是链接到 create 方法,求解决!代码如下: $items/index.html.erb
<h2>Incomplete</h2>
<%= form_for(:item) do |f| %>
<% @items.each do |item| %>
<% if !item.finish %>
<p>
<%= f.submit 'Finish' %>
<%= item.name %>
<%= item.finish %>
<%= link_to "(Remove)", item, method: :delete %>
</p>
<% end %>
<% end %>
<% end %>
$items_controller.rb
def index
@item = Item.new
@items = Item.all
end
def new
@item = Item.new
end
def create
@item = Item.new(params[:item])
# @item.save
if @item.save
redirect_to root_path
else
render 'new'
end
end
def destroy
@item = Item.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to items_url }
format.json { head :no_content }
end
end
def update
end
end