Reputation: 117
I am a new rails developer and I have a rails app where I allow users to make a post while allowing them an option to check a checkbox. I want to be able to manually review all posts that are checked by users during their posting process. Right now everything is getting posted successfully but I want this review process in place for all checkmarked posts. What is the simplest and easiest way to put this review in place?
Here's what the post controller is right now
def create
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "Shared!"
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end
Upvotes: 0
Views: 781
Reputation: 988
For this, you better keep data-type of "reviewed" field as "boolean" (in migration files).
For putting up check box in your view, check: http://guides.rubyonrails.org/form_helpers.html#helpers-for-generating-form-elements
Also, note that IF YOU WANT to validate the presence of boolean field (where the real values are true and false), you can't use:
validate :field_name, :presence => true
this is due to the way Object#blank? handles boolean values. false.blank? # => true In this case, you can use:
validates :field_name, :allow_nil => true, :inclusion => {:in => [true, false]}
You can omit ":allow_nil => true", from above validation statement if you are assigning false as default value to newly created posts (and that you would require to do before validations get triggered).
Upvotes: 1
Reputation: 1866
I'm not talking in perspective of RoR, but in general implementation
In the database, add a field called 'Reviewed'. It holds a 'Y' or 'N'. default should be 'Y'.
Now, while saving or Updating the post make the check box flag a Y or N into this field and in the Model, Only query for posts with 'Reviewed' = 'Y'.
This way, if the user does not check the box, the default value is 'Y' and the posts shows up, but if the user checks the box, the value shall be 'N' and you develop a review interface (i hope you already did) where after review, you mark it to 'Y'.
This is as simple as it can get. If you need to add more information on who review it, when it is reviewed etc, you can add more DB fields to store that.
Update
I work on ColdFusion technology and we have CFWheels framework which is inspired by RoR.
Based on the experience, I suggest you to use the Form helpers.
Try this code
<%= check_box_tag(:post_reviewed) %>
<%= label_tag(:post_reviewed, "Review this post") %>
And for more details, read check box tag in ruby on rails and check box form helper
Upvotes: 0