Ruby on Rails revise instead of edit
I am still a bit new to RoR, and am using scaffolding to generate CRUD interfaces for data.
I am using devise for user authentication, and want to allow a user that owns a specific entry to edit or delete, but protect that data from other users. However, I would like to allow a different user to revise or create new versions.
So if a user that attempts to edit should appear as if they are editing, but when they submit, the controller should actually generate a new entry (and potentially specify the parent_id of the entry it derived from).
Any help on implementati开发者_开发技巧on is greatly appreciated.
Also look into Ancestry, it's a really nice library to help with versioning.
Here is my solution that I came up with. I am a ruby newb, so I assume that I can just call the create function with the same params but wasn't sure how to do that, so I just duplicated code:
def update
@section = Section.find(params[:id])
if @section.owner == current_user.id
respond_to do |format|
if @section.update_attributes(params[:section])
format.html { redirect_to(@section, :notice => 'Section was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @section.errors, :status => :unprocessable_entity }
end
end
else
# REVISE
@childsection = Section.new(params[:section])
respond_to do |format|
if @childsection.save
format.html { redirect_to(@childsection, :notice => 'Section was successfully revised.') }
format.xml { render :xml => @childsection, :status => :created, :location => @childsection }
else
format.html { render :action => "new" }
format.xml { render :xml => @childsection.errors, :status => :unprocessable_entity }
end
end
end
end
精彩评论