In my contactlist CRUD application, all the contacts are shown properly when i type the following URL: localhost/contact
However, I want to have two options for each contact- either to update the information or to delete the record. So once i click on contact,say- “Chetan”, i want to go to a ‘showmain’ page with two options- Update(linking me to an update page) and Delete(linking me to a del page).
My controller is as follows:-
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
And my showmain.html.erb page where i am getting the syntax error(showmain.html.erb:14: syntax error, unexpected keyword_ensure, expecting $end) is as follows:-
<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>
Need some help to remove the error..
My show.html.erb is as follows:-
<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>
1 Answer