开发者

How to implement radio buttons for specific scenario in Rails

I've searched around but haven't found an answer, but I'm also not sure what terms to search for, so please point me to similar questions if you know of any.

Scenario: I have posts. I have an admin side and a public side. I want a single post displayed on the public side front page.

On the admin side, on the posts index page, I would like to be able to select a radio button next to the post I want displayed on the front page. My guess is I would need a form, radio buttons for each of the posts, and an update button. But that's as far as I'm able to get on my own.

I've looked into virtual attributes, but I don't think that's what I need, or is it?开发者_开发百科 What's a good way to accomplish what I need?

Thanks in advance everyone.


You could add a "public" field to your posts model, and access it via a checkbox on the edit post form. In your controller on a create or update, check to see if the public field is true, if so then you'll need to run something like Post.update_all(['public = ?', false], ['id != ?', current_post_id]) in order to revert any existing public posts back to private.

Alternatively, you could create a simple model for AppData (:id => :integer, :key => :string, :value => :string) and store the id of the currently selected public post on a row in that table. To keep it simple, you'll want to serialize the :value field in your model. In this case, you would use just an unattached checkbox_tag in your view and capture that value from the params hash. If it's checked, then you'll need to update the AppData row with the new id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜