Rails3 - Given a Form that allows for Updating/Creating - How Delete/Destroy a record?
I have the following for which allows for creating or updating a record:
<%=开发者_C百科form_for [:project, @permission], :remote => true do |f| %>
... You can change roles in the form... (This works great for new or existing records)
<% end %>
Inside the form, I want to provide an option to delete/destroy the permission. So I added:
<%= link_to 'remove from the team', [:project, @permission], :confirm => 'Are you sure?', :method => :delete %>
But that's not working, erroring...
It renders as follows:
<a rel="nofollow" data-method="delete" data-confirm="Are you sure?" href="/projects/4/permissions/useronproject">remove from the team</a>
Error Message: NoMethodError in PermissionsController#destroy undefined method `destroy' for nil:NilClass
Also the params being passed are only: _method delete auth token....
So I'm not sure how Rails would know what to delete?
regarding the permissionsController. I do have a destory:
def destroy
@permission.destroy
respond_to do |format|
format.js
end
end
Any thoughts on this one? thanks
It seems that the permission id in your url is not an integer but the name (or a permalink) of the permission.
Have you set something like
def to_param
permalink
end
in your permission model?
The error returned tells that the @permission is set to nil (meaning that no permission is found with such id). In your destroy action you need to retrieve your permission by this permalink/name, something like:
def destroy
@permission = Permission.find_by_permalink(params[:id])
@permission.destroy
respond_to do |format|
format.js
end
end
精彩评论