0

I have a model in which I have a boolean attribute, published. In my Post controller I have added it to my permitted attributes. Like this:

params.require(:post).permit([...], :published)

My index action that should list all published posts looks like this:

def index
    if session[:user_id]
      @posts = Post.paginate(:page => params[:page])
    else
      @posts = Post.where("published = 1").paginate(:page => params[:page])
    end
end

And lastly my form looks like this:

= form_for @post, :html => { :multipart => true } do |f|
  [...]
  .field
    = f.label :published, "Publicera:"
    = f.check_box :published      
  .actions
    = f.submit

Currently no posts is listen on my index page, even if I either the new or updat view checks the checkbox. And I'm not sure how to fix it, any ideas?

3
  • Have you checked that the query is generated correctly? log/development.log will show you the SQL that's executed, and you can always test this against your database independently to verify it's working correctly. Commented Jul 12, 2013 at 17:59
  • @tadman My log for my update action shows this: Processing by PostsController#update as HTML Parameters: {[...], "published"=>"1"}, "commit"=>"Uppdatera inlägg", "id"=>"3"} Redirected to http://localhost:3000/posts/3 Completed 302 Found in 13ms (ActiveRecord: 1.9ms). 303, is a problem, right? Not many clues here though... Commented Jul 12, 2013 at 18:06
  • You should have some kind of query in there, too, not just the parameters. Commented Jul 12, 2013 at 20:17

1 Answer 1

1

The problem seems be be with my where predicate. This worked:

@posts = Post.where(:published => true).paginate(:page => params[:page])
Sign up to request clarification or add additional context in comments.

1 Comment

"published=1" is a MySQL thing, so if you're using another database that might not work. :published => true should be equivalent on all platforms that support booleans or boolean emulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.