在我的联系人列表 CRUD 应用程序中,当我键入以下 URL 时,所有联系人都会正确显示:localhost/contact
但是,我希望每个联系人有两个选项 - 更新信息或删除记录。因此,一旦我点击联系人,比如说“Chetan”,我想进入一个带有两个选项的“showmain”页面——更新(将我链接到更新页面)和删除(将我链接到删除页面)。
我的控制器如下:-
class ContactController < ApplicationController
def index
@contacts=Contact.find(:all)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @contacts }
end
end
def show
@contact=Contact.find(params[:id])
end
def del
@contact=Contact.find(params[:id])
end
def new
@contact=Contact.new
end
def create
@contact=Contact.new(params[:contact])
if @contact.save!
redirect_to :action => "index"
else
render :action => "new"
end
end
def showmain
respond_to do |format|
format.html # index.html.erb
format.json
# @contact=Contact.new(params[:contact])
end
def update
@contact=Contact.find(params[:id])
@contact.attributes= params[:contact]
@contact.save!
redirect_to :action => "index"
end
def delete
@contact=Contact.find(params[:id])
@contact.destroy
redirect_to :action => "index"
end
end
end
我的 showmain.html.erb 页面出现语法错误(showmain.html.erb:14: syntax error, unexpected keyword_ensure, expecting $end)如下:-
<h1>Do you want to Update or Delete the Attributes? </h1>
<ul>
<li>
<%= link_to "Update" ,:action=>'show',:id => @contact -%>
</li>
<li>
<%= link_to "Delete" ,:action=>'del',:id => @contact -%>
</li>
</ul>
<% end %>
<p>
<%= link_to "Back", {:action => 'index'} -%>
</p>
需要一些帮助来消除错误..
我的 show.html.erb 如下:-
<h1>View/Edit Contact</h1>
<% link_to "Update" , contact_path(@contact) do |f| -%
<%= f.label :first_name %>:
<%= f.text_field :first_name %><br />
<%= f.label :last_name %>:
<%= f.text_field :last_name %><br />
<%= f.label :address %>:
<%= f.text_field :address %><br />
<%= f.label :city %>:
<%= f.text_field :city %><br />
<%= f.label :state %>:
<%= f.text_field :state %><br />
<%= f.label :country %>:
<%= f.text_field :country %><br />
<%= f.label :phone %>:
<%= f.text_field :phone %><br />
<%= f.label :email %>:
<%= f.text_field :email %><br />
<%= f.submit "Update" %>
<% end %>
<p>
<%= link_to 'Back', {:action => 'index'} %>
</p>