It compares the actual password stored in the database (@submission.password) with the password entered by the user from the view (params[:password]). If they are equal, the submission is deleted.
This is the string comparison method.
def compare_password
if @submission.password == params[:password] # This line is wrong.
@submission = Submission.find(params[:id])
@submission.destroy
redirect_to(@submission, :notice => 'Listing deleted successfully')
else
redirect_to(@submission, :alert => 'Password is incorrect.')
end
end
This is the view.
<%form开发者_开发知识库_tag "/submissions/compare_password" do%>
<%=text_field_tag :password, params[:password]%>
<%=submit_tag "Delete"%>
<%end%>
Execution error I get is this.
undefined method `password' for nil:NilClass
edit
Originally my routes.rb looked like this
resources :submissions do
collection do
post :compare_password
end
end
Now I changed to
resources :submissions do
member do
post :compare_password
end
end
@submission is nil - you need to find this before checking the password value -
def compare_password
@submission = Submission.find(params[:id]) # it is nil till you get it here
if @submission && (@submission.password == params[:password])
@submission.destroy
redirect_to(@submission, :notice => 'Listing deleted successfully')
else
redirect_to(@submission, :alert => 'Password is incorrect.')
end
end
精彩评论