Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this particular instance they look like status flags that could be handled a few ways, but you've asked about select options so here's a solution for that method.</p> <p>Because you don't specify if you need to keep the values already in the table I've detailed a method that allows you to keep them by converting them to IDs (assuming they are currently strings), if this is not relevant then follow only the bold instructions.</p> <p><strong>Create a PostStatus resource</strong> (model, migrate, controller/view if you need the ability to change them).</p> <p><strong>Define the relationships</strong></p> <pre><code>PostStatus has_many :posts Post belongs_to :post_status </code></pre> <p><strong>Add values to your PostStatus table</strong> (if you have a live system with strings in the table you should match the existing post status strings here to allow you to convert the data (detailed below).</p> <p><strong>Change column name to post_status_id in the Post table, change its type to int. If this isn't live then just redo the migrate with the column as integer.</strong> If it is a live system you'll need to convert your data into a new column instead of just changing its type, the below is a suggested method.</p> <pre><code>add_column :posts, :post_status_id_tmp, :int Post.reset_column_information # make the new column available to model methods Post.all.each do |post| # Assuming you have a string with the option text currently: post.post_status_id_tmp = PostStatus.find_by_name(post.post_status).id post.save end remove_column :posts, :post_status rename_column :posts, :post_status_tmp, :post_status_id </code></pre> <p><strong>In your post form add a selectbox.</strong></p> <pre><code>&lt;%= form.collection_select :post_status_id, PostStatus.all, :id, :name %&gt; </code></pre> <p>That should at the least get you started!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload