Skip to content

Instantly share code, notes, and snippets.

@rishighan
Created August 26, 2014 12:01
Show Gist options
  • Save rishighan/fd486836e87cde359478 to your computer and use it in GitHub Desktop.
Save rishighan/fd486836e87cde359478 to your computer and use it in GitHub Desktop.
update action in posts controller
def update
respond_to do |format|
case params[:commit]
when "Save Draft"
@post.update_attribute(:is_draft, "yes")
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
when "Update Post"
@post.update_attribute(:is_draft, "no")
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
@rishighan
Copy link
Author

The update action takes an id.
As opposed to the update_attributes method that assumes you are already working with the object.

So something like

Post.update(@post.id, post_params)

should work, given that you get the @post.id from the form you are working with and post_params is the whitelisted set of attributes you want to update the Post model with.

@rishighan
Copy link
Author

Also because YOLO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment