RAILS - How do I add a select tag and put all my category items












0















So I have a scaffold for both blogs and post_category.



I made an association with both of them. Here's my schema:



  create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.integer "status", default: 0
t.bigint "post_category_id"
t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
end

create_table "post_categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end


So simply I created a couple of post categories and when I try to make an association with blog items so whenever I create a new blog I can display a select statement and choose my preferred category for the blog item I am trying to create but I don't know how to display all categories on all forms and on the index.html.erb file:



  <div class="field">
<%= form.label :category %>
<%= form.collection_select :post_category, PostCategory.all %>
</div>


How can I achieve this? And make sure that it saves the data as well?










share|improve this question



























    0















    So I have a scaffold for both blogs and post_category.



    I made an association with both of them. Here's my schema:



      create_table "blogs", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "slug"
    t.integer "status", default: 0
    t.bigint "post_category_id"
    t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
    t.index ["slug"], name: "index_blogs_on_slug", unique: true
    end

    create_table "post_categories", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    end


    So simply I created a couple of post categories and when I try to make an association with blog items so whenever I create a new blog I can display a select statement and choose my preferred category for the blog item I am trying to create but I don't know how to display all categories on all forms and on the index.html.erb file:



      <div class="field">
    <%= form.label :category %>
    <%= form.collection_select :post_category, PostCategory.all %>
    </div>


    How can I achieve this? And make sure that it saves the data as well?










    share|improve this question

























      0












      0








      0








      So I have a scaffold for both blogs and post_category.



      I made an association with both of them. Here's my schema:



        create_table "blogs", force: :cascade do |t|
      t.string "title"
      t.text "body"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.string "slug"
      t.integer "status", default: 0
      t.bigint "post_category_id"
      t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
      t.index ["slug"], name: "index_blogs_on_slug", unique: true
      end

      create_table "post_categories", force: :cascade do |t|
      t.string "name"
      t.text "description"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      end


      So simply I created a couple of post categories and when I try to make an association with blog items so whenever I create a new blog I can display a select statement and choose my preferred category for the blog item I am trying to create but I don't know how to display all categories on all forms and on the index.html.erb file:



        <div class="field">
      <%= form.label :category %>
      <%= form.collection_select :post_category, PostCategory.all %>
      </div>


      How can I achieve this? And make sure that it saves the data as well?










      share|improve this question














      So I have a scaffold for both blogs and post_category.



      I made an association with both of them. Here's my schema:



        create_table "blogs", force: :cascade do |t|
      t.string "title"
      t.text "body"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.string "slug"
      t.integer "status", default: 0
      t.bigint "post_category_id"
      t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
      t.index ["slug"], name: "index_blogs_on_slug", unique: true
      end

      create_table "post_categories", force: :cascade do |t|
      t.string "name"
      t.text "description"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      end


      So simply I created a couple of post categories and when I try to make an association with blog items so whenever I create a new blog I can display a select statement and choose my preferred category for the blog item I am trying to create but I don't know how to display all categories on all forms and on the index.html.erb file:



        <div class="field">
      <%= form.label :category %>
      <%= form.collection_select :post_category, PostCategory.all %>
      </div>


      How can I achieve this? And make sure that it saves the data as well?







      ruby-on-rails






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 1:51









      Marc SolvaMarc Solva

      283410




      283410
























          1 Answer
          1






          active

          oldest

          votes


















          1














          as per this reference, the format is



          collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})


          and for your code, it can be like this



          <%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>


          update 2:



          inside your blog_controllers



          # white list parameter 
          def blog_params
          params.require(:blog).permit(
          :post_category_id,
          ... others fields
          )
          end





          share|improve this answer


























          • Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

            – Marc Solva
            Jan 2 at 3:10













          • you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

            – widjajayd
            Jan 2 at 3:22











          • <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

            – Marc Solva
            Jan 2 at 3:28











          • This one works but when I check on the rails console it doesn't save the data.

            – Marc Solva
            Jan 2 at 3:29













          • make sure you have white list post_category_id inside your controller (see my update number 2 above)

            – widjajayd
            Jan 2 at 3:39











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000382%2frails-how-do-i-add-a-select-tag-and-put-all-my-category-items%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          as per this reference, the format is



          collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})


          and for your code, it can be like this



          <%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>


          update 2:



          inside your blog_controllers



          # white list parameter 
          def blog_params
          params.require(:blog).permit(
          :post_category_id,
          ... others fields
          )
          end





          share|improve this answer


























          • Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

            – Marc Solva
            Jan 2 at 3:10













          • you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

            – widjajayd
            Jan 2 at 3:22











          • <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

            – Marc Solva
            Jan 2 at 3:28











          • This one works but when I check on the rails console it doesn't save the data.

            – Marc Solva
            Jan 2 at 3:29













          • make sure you have white list post_category_id inside your controller (see my update number 2 above)

            – widjajayd
            Jan 2 at 3:39
















          1














          as per this reference, the format is



          collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})


          and for your code, it can be like this



          <%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>


          update 2:



          inside your blog_controllers



          # white list parameter 
          def blog_params
          params.require(:blog).permit(
          :post_category_id,
          ... others fields
          )
          end





          share|improve this answer


























          • Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

            – Marc Solva
            Jan 2 at 3:10













          • you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

            – widjajayd
            Jan 2 at 3:22











          • <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

            – Marc Solva
            Jan 2 at 3:28











          • This one works but when I check on the rails console it doesn't save the data.

            – Marc Solva
            Jan 2 at 3:29













          • make sure you have white list post_category_id inside your controller (see my update number 2 above)

            – widjajayd
            Jan 2 at 3:39














          1












          1








          1







          as per this reference, the format is



          collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})


          and for your code, it can be like this



          <%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>


          update 2:



          inside your blog_controllers



          # white list parameter 
          def blog_params
          params.require(:blog).permit(
          :post_category_id,
          ... others fields
          )
          end





          share|improve this answer















          as per this reference, the format is



          collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})


          and for your code, it can be like this



          <%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>


          update 2:



          inside your blog_controllers



          # white list parameter 
          def blog_params
          params.require(:blog).permit(
          :post_category_id,
          ... others fields
          )
          end






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 3:38

























          answered Jan 2 at 2:45









          widjajaydwidjajayd

          3,73331425




          3,73331425













          • Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

            – Marc Solva
            Jan 2 at 3:10













          • you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

            – widjajayd
            Jan 2 at 3:22











          • <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

            – Marc Solva
            Jan 2 at 3:28











          • This one works but when I check on the rails console it doesn't save the data.

            – Marc Solva
            Jan 2 at 3:29













          • make sure you have white list post_category_id inside your controller (see my update number 2 above)

            – widjajayd
            Jan 2 at 3:39



















          • Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

            – Marc Solva
            Jan 2 at 3:10













          • you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

            – widjajayd
            Jan 2 at 3:22











          • <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

            – Marc Solva
            Jan 2 at 3:28











          • This one works but when I check on the rails console it doesn't save the data.

            – Marc Solva
            Jan 2 at 3:29













          • make sure you have white list post_category_id inside your controller (see my update number 2 above)

            – widjajayd
            Jan 2 at 3:39

















          Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

          – Marc Solva
          Jan 2 at 3:10







          Upon saving the form I am havint this error: 1 error prohibited this blog from being saved: Post category must exist

          – Marc Solva
          Jan 2 at 3:10















          you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

          – widjajayd
          Jan 2 at 3:22





          you can add couple of content, open your terminal, run rails console and type this PostCategory.create(name: "Cat 1", description: "Cat 1")

          – widjajayd
          Jan 2 at 3:22













          <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

          – Marc Solva
          Jan 2 at 3:28





          <div class="field"> <%= form.label :category %> <%= form.collection_select(:post_category_id, PostCategory.all, :id, :name)%> </div>

          – Marc Solva
          Jan 2 at 3:28













          This one works but when I check on the rails console it doesn't save the data.

          – Marc Solva
          Jan 2 at 3:29







          This one works but when I check on the rails console it doesn't save the data.

          – Marc Solva
          Jan 2 at 3:29















          make sure you have white list post_category_id inside your controller (see my update number 2 above)

          – widjajayd
          Jan 2 at 3:39





          make sure you have white list post_category_id inside your controller (see my update number 2 above)

          – widjajayd
          Jan 2 at 3:39




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000382%2frails-how-do-i-add-a-select-tag-and-put-all-my-category-items%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith