Class name in html.erb file separated











up vote
1
down vote

favorite












I have an if statement inside a loop to change a variable (named var_class) for a class name. How can I make it continuous string?



Here is the code:



<% j = 0%>
<% @question.each do |index| %>
<% var_class = ""%>
<% j == 0 ? var_class = "tab-pane fade active show" : var_class = "tab-pane fade" %>
<div class=<%=var_class %> style='height: 444px; overflow-y: auto;' id=<%="question#{j+=1}"%>>


But when i viewed the html in chrome inspect, it's not included as a continuous string but instead is separated when there is space. Like such:



class= "tab-pane" fade active show


but i want it to be



class = "tab-pane fade active show"


I've tried <div class=<%=#{var_class} %> and <div class=<%="var_class" %> and derivatives of that. Can someone help?










share|improve this question







New contributor




Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    I have an if statement inside a loop to change a variable (named var_class) for a class name. How can I make it continuous string?



    Here is the code:



    <% j = 0%>
    <% @question.each do |index| %>
    <% var_class = ""%>
    <% j == 0 ? var_class = "tab-pane fade active show" : var_class = "tab-pane fade" %>
    <div class=<%=var_class %> style='height: 444px; overflow-y: auto;' id=<%="question#{j+=1}"%>>


    But when i viewed the html in chrome inspect, it's not included as a continuous string but instead is separated when there is space. Like such:



    class= "tab-pane" fade active show


    but i want it to be



    class = "tab-pane fade active show"


    I've tried <div class=<%=#{var_class} %> and <div class=<%="var_class" %> and derivatives of that. Can someone help?










    share|improve this question







    New contributor




    Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have an if statement inside a loop to change a variable (named var_class) for a class name. How can I make it continuous string?



      Here is the code:



      <% j = 0%>
      <% @question.each do |index| %>
      <% var_class = ""%>
      <% j == 0 ? var_class = "tab-pane fade active show" : var_class = "tab-pane fade" %>
      <div class=<%=var_class %> style='height: 444px; overflow-y: auto;' id=<%="question#{j+=1}"%>>


      But when i viewed the html in chrome inspect, it's not included as a continuous string but instead is separated when there is space. Like such:



      class= "tab-pane" fade active show


      but i want it to be



      class = "tab-pane fade active show"


      I've tried <div class=<%=#{var_class} %> and <div class=<%="var_class" %> and derivatives of that. Can someone help?










      share|improve this question







      New contributor




      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have an if statement inside a loop to change a variable (named var_class) for a class name. How can I make it continuous string?



      Here is the code:



      <% j = 0%>
      <% @question.each do |index| %>
      <% var_class = ""%>
      <% j == 0 ? var_class = "tab-pane fade active show" : var_class = "tab-pane fade" %>
      <div class=<%=var_class %> style='height: 444px; overflow-y: auto;' id=<%="question#{j+=1}"%>>


      But when i viewed the html in chrome inspect, it's not included as a continuous string but instead is separated when there is space. Like such:



      class= "tab-pane" fade active show


      but i want it to be



      class = "tab-pane fade active show"


      I've tried <div class=<%=#{var_class} %> and <div class=<%="var_class" %> and derivatives of that. Can someone help?







      html ruby-on-rails twitter-bootstrap erb






      share|improve this question







      New contributor




      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Danz Tim

      82




      82




      New contributor




      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Danz Tim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I think that you're missing the quotes of class and id attributes.



          <div class='<%=var_class %>' style='height: 444px; overflow-y: auto;' id='<%="question#{j+=1}"%>'>





          share|improve this answer





















          • This fixed the problem thank you :)
            – Danz Tim
            yesterday


















          up vote
          1
          down vote













          Messias' answer is the basics of it. Without the quotes around the string, the DOM is stopping the classes when it hits a space. It thinks the other items are attributes on the div tag.



          In a best-practices kind of way, we typically want to avoid local assigns (where you set a variable) in the rails view. This really wants to be a helper or a decorator (if you really need that level of functionality and testability).



          [The following is pseudo code that I haven't tested, so there may be some minor errors]



          In your ERB view:



          <% @questions.each do |index| %>
          <div class="<%= classy(index) %>" id="[TBD see below]">
          <% end %>


          I'd suggest finding a more semantic HTML tag than div if possible. Iterators (.each) tend to be a list of objects, so perhaps a UL (unordered list) or OL (ordered list) with LIs inside might be a better choice. ULs give you buttons and OLs give you numbers by default, but those can be changed.



          You also probably want to avoid the # sign in a ID name, as you would call an ID in a css file with #name. So #name#2 will likely give you some unexpected and messy results in the DOM. You likely want to use each_ with _index to get both the array and the index value. Pretty good explanation of the differences here: What is the "right" way to iterate through an array in Ruby?



          <ul class="questions">
          <% @questions.each_with_index do |question,index| %>
          <li class="<%= classy(question, index) %>" id="question_<%= index %>">
          [CONTENT]
          </li>
          <% end %>
          </ul>


          The helper method in whatever_helper.rb (matching the class name or application if site wide):



          def classy(question, index)
          if index == 0
          "tab-pane fade active show"
          else
          "tab-pane fade"
          end
          end


          And while we're at it, lets get presentation markup out of the view. In your CSS file:



          .tab-pane.fade {
          height: 444px;
          overflow-y: auto;
          }


          If you're trying to achieve something different, let us know. This was my best guess based on reading the question and code.






          share|improve this answer




























            up vote
            0
            down vote













            In Ruby you can use Enumerable#each_with_index to iterate through a collection with an index:



            <% @question.each_with_index do |value, index| %>

            <% end %>

            <% end %>


            In general in Ruby if you are using .each or any iterator with an external mutating variable you are doing it wrong.



            You can also clean up the view by using content_tag:



            <% @question.each_with_index do |value, index| %>
            <%
            classes = ["tab-pane", "fade"]
            classes = classes + ["active", "show"] if index == 0
            %>
            <%= content_tag :div, class: classes, id: "question#{index+1}" do %>

            <% end %>
            <% end %>


            The same approach can be used with straight ERB inpolation if you just join an array of classes:



            <div class="<%= classes.join(" ")%>" ...





            share|improve this answer





















              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',
              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
              });


              }
              });






              Danz Tim is a new contributor. Be nice, and check out our Code of Conduct.










               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238178%2fclass-name-in-html-erb-file-separated%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              I think that you're missing the quotes of class and id attributes.



              <div class='<%=var_class %>' style='height: 444px; overflow-y: auto;' id='<%="question#{j+=1}"%>'>





              share|improve this answer





















              • This fixed the problem thank you :)
                – Danz Tim
                yesterday















              up vote
              1
              down vote



              accepted










              I think that you're missing the quotes of class and id attributes.



              <div class='<%=var_class %>' style='height: 444px; overflow-y: auto;' id='<%="question#{j+=1}"%>'>





              share|improve this answer





















              • This fixed the problem thank you :)
                – Danz Tim
                yesterday













              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              I think that you're missing the quotes of class and id attributes.



              <div class='<%=var_class %>' style='height: 444px; overflow-y: auto;' id='<%="question#{j+=1}"%>'>





              share|improve this answer












              I think that you're missing the quotes of class and id attributes.



              <div class='<%=var_class %>' style='height: 444px; overflow-y: auto;' id='<%="question#{j+=1}"%>'>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 days ago









              Messias Tayllan

              547




              547












              • This fixed the problem thank you :)
                – Danz Tim
                yesterday


















              • This fixed the problem thank you :)
                – Danz Tim
                yesterday
















              This fixed the problem thank you :)
              – Danz Tim
              yesterday




              This fixed the problem thank you :)
              – Danz Tim
              yesterday












              up vote
              1
              down vote













              Messias' answer is the basics of it. Without the quotes around the string, the DOM is stopping the classes when it hits a space. It thinks the other items are attributes on the div tag.



              In a best-practices kind of way, we typically want to avoid local assigns (where you set a variable) in the rails view. This really wants to be a helper or a decorator (if you really need that level of functionality and testability).



              [The following is pseudo code that I haven't tested, so there may be some minor errors]



              In your ERB view:



              <% @questions.each do |index| %>
              <div class="<%= classy(index) %>" id="[TBD see below]">
              <% end %>


              I'd suggest finding a more semantic HTML tag than div if possible. Iterators (.each) tend to be a list of objects, so perhaps a UL (unordered list) or OL (ordered list) with LIs inside might be a better choice. ULs give you buttons and OLs give you numbers by default, but those can be changed.



              You also probably want to avoid the # sign in a ID name, as you would call an ID in a css file with #name. So #name#2 will likely give you some unexpected and messy results in the DOM. You likely want to use each_ with _index to get both the array and the index value. Pretty good explanation of the differences here: What is the "right" way to iterate through an array in Ruby?



              <ul class="questions">
              <% @questions.each_with_index do |question,index| %>
              <li class="<%= classy(question, index) %>" id="question_<%= index %>">
              [CONTENT]
              </li>
              <% end %>
              </ul>


              The helper method in whatever_helper.rb (matching the class name or application if site wide):



              def classy(question, index)
              if index == 0
              "tab-pane fade active show"
              else
              "tab-pane fade"
              end
              end


              And while we're at it, lets get presentation markup out of the view. In your CSS file:



              .tab-pane.fade {
              height: 444px;
              overflow-y: auto;
              }


              If you're trying to achieve something different, let us know. This was my best guess based on reading the question and code.






              share|improve this answer

























                up vote
                1
                down vote













                Messias' answer is the basics of it. Without the quotes around the string, the DOM is stopping the classes when it hits a space. It thinks the other items are attributes on the div tag.



                In a best-practices kind of way, we typically want to avoid local assigns (where you set a variable) in the rails view. This really wants to be a helper or a decorator (if you really need that level of functionality and testability).



                [The following is pseudo code that I haven't tested, so there may be some minor errors]



                In your ERB view:



                <% @questions.each do |index| %>
                <div class="<%= classy(index) %>" id="[TBD see below]">
                <% end %>


                I'd suggest finding a more semantic HTML tag than div if possible. Iterators (.each) tend to be a list of objects, so perhaps a UL (unordered list) or OL (ordered list) with LIs inside might be a better choice. ULs give you buttons and OLs give you numbers by default, but those can be changed.



                You also probably want to avoid the # sign in a ID name, as you would call an ID in a css file with #name. So #name#2 will likely give you some unexpected and messy results in the DOM. You likely want to use each_ with _index to get both the array and the index value. Pretty good explanation of the differences here: What is the "right" way to iterate through an array in Ruby?



                <ul class="questions">
                <% @questions.each_with_index do |question,index| %>
                <li class="<%= classy(question, index) %>" id="question_<%= index %>">
                [CONTENT]
                </li>
                <% end %>
                </ul>


                The helper method in whatever_helper.rb (matching the class name or application if site wide):



                def classy(question, index)
                if index == 0
                "tab-pane fade active show"
                else
                "tab-pane fade"
                end
                end


                And while we're at it, lets get presentation markup out of the view. In your CSS file:



                .tab-pane.fade {
                height: 444px;
                overflow-y: auto;
                }


                If you're trying to achieve something different, let us know. This was my best guess based on reading the question and code.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Messias' answer is the basics of it. Without the quotes around the string, the DOM is stopping the classes when it hits a space. It thinks the other items are attributes on the div tag.



                  In a best-practices kind of way, we typically want to avoid local assigns (where you set a variable) in the rails view. This really wants to be a helper or a decorator (if you really need that level of functionality and testability).



                  [The following is pseudo code that I haven't tested, so there may be some minor errors]



                  In your ERB view:



                  <% @questions.each do |index| %>
                  <div class="<%= classy(index) %>" id="[TBD see below]">
                  <% end %>


                  I'd suggest finding a more semantic HTML tag than div if possible. Iterators (.each) tend to be a list of objects, so perhaps a UL (unordered list) or OL (ordered list) with LIs inside might be a better choice. ULs give you buttons and OLs give you numbers by default, but those can be changed.



                  You also probably want to avoid the # sign in a ID name, as you would call an ID in a css file with #name. So #name#2 will likely give you some unexpected and messy results in the DOM. You likely want to use each_ with _index to get both the array and the index value. Pretty good explanation of the differences here: What is the "right" way to iterate through an array in Ruby?



                  <ul class="questions">
                  <% @questions.each_with_index do |question,index| %>
                  <li class="<%= classy(question, index) %>" id="question_<%= index %>">
                  [CONTENT]
                  </li>
                  <% end %>
                  </ul>


                  The helper method in whatever_helper.rb (matching the class name or application if site wide):



                  def classy(question, index)
                  if index == 0
                  "tab-pane fade active show"
                  else
                  "tab-pane fade"
                  end
                  end


                  And while we're at it, lets get presentation markup out of the view. In your CSS file:



                  .tab-pane.fade {
                  height: 444px;
                  overflow-y: auto;
                  }


                  If you're trying to achieve something different, let us know. This was my best guess based on reading the question and code.






                  share|improve this answer












                  Messias' answer is the basics of it. Without the quotes around the string, the DOM is stopping the classes when it hits a space. It thinks the other items are attributes on the div tag.



                  In a best-practices kind of way, we typically want to avoid local assigns (where you set a variable) in the rails view. This really wants to be a helper or a decorator (if you really need that level of functionality and testability).



                  [The following is pseudo code that I haven't tested, so there may be some minor errors]



                  In your ERB view:



                  <% @questions.each do |index| %>
                  <div class="<%= classy(index) %>" id="[TBD see below]">
                  <% end %>


                  I'd suggest finding a more semantic HTML tag than div if possible. Iterators (.each) tend to be a list of objects, so perhaps a UL (unordered list) or OL (ordered list) with LIs inside might be a better choice. ULs give you buttons and OLs give you numbers by default, but those can be changed.



                  You also probably want to avoid the # sign in a ID name, as you would call an ID in a css file with #name. So #name#2 will likely give you some unexpected and messy results in the DOM. You likely want to use each_ with _index to get both the array and the index value. Pretty good explanation of the differences here: What is the "right" way to iterate through an array in Ruby?



                  <ul class="questions">
                  <% @questions.each_with_index do |question,index| %>
                  <li class="<%= classy(question, index) %>" id="question_<%= index %>">
                  [CONTENT]
                  </li>
                  <% end %>
                  </ul>


                  The helper method in whatever_helper.rb (matching the class name or application if site wide):



                  def classy(question, index)
                  if index == 0
                  "tab-pane fade active show"
                  else
                  "tab-pane fade"
                  end
                  end


                  And while we're at it, lets get presentation markup out of the view. In your CSS file:



                  .tab-pane.fade {
                  height: 444px;
                  overflow-y: auto;
                  }


                  If you're trying to achieve something different, let us know. This was my best guess based on reading the question and code.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  John Athayde

                  311110




                  311110






















                      up vote
                      0
                      down vote













                      In Ruby you can use Enumerable#each_with_index to iterate through a collection with an index:



                      <% @question.each_with_index do |value, index| %>

                      <% end %>

                      <% end %>


                      In general in Ruby if you are using .each or any iterator with an external mutating variable you are doing it wrong.



                      You can also clean up the view by using content_tag:



                      <% @question.each_with_index do |value, index| %>
                      <%
                      classes = ["tab-pane", "fade"]
                      classes = classes + ["active", "show"] if index == 0
                      %>
                      <%= content_tag :div, class: classes, id: "question#{index+1}" do %>

                      <% end %>
                      <% end %>


                      The same approach can be used with straight ERB inpolation if you just join an array of classes:



                      <div class="<%= classes.join(" ")%>" ...





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        In Ruby you can use Enumerable#each_with_index to iterate through a collection with an index:



                        <% @question.each_with_index do |value, index| %>

                        <% end %>

                        <% end %>


                        In general in Ruby if you are using .each or any iterator with an external mutating variable you are doing it wrong.



                        You can also clean up the view by using content_tag:



                        <% @question.each_with_index do |value, index| %>
                        <%
                        classes = ["tab-pane", "fade"]
                        classes = classes + ["active", "show"] if index == 0
                        %>
                        <%= content_tag :div, class: classes, id: "question#{index+1}" do %>

                        <% end %>
                        <% end %>


                        The same approach can be used with straight ERB inpolation if you just join an array of classes:



                        <div class="<%= classes.join(" ")%>" ...





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          In Ruby you can use Enumerable#each_with_index to iterate through a collection with an index:



                          <% @question.each_with_index do |value, index| %>

                          <% end %>

                          <% end %>


                          In general in Ruby if you are using .each or any iterator with an external mutating variable you are doing it wrong.



                          You can also clean up the view by using content_tag:



                          <% @question.each_with_index do |value, index| %>
                          <%
                          classes = ["tab-pane", "fade"]
                          classes = classes + ["active", "show"] if index == 0
                          %>
                          <%= content_tag :div, class: classes, id: "question#{index+1}" do %>

                          <% end %>
                          <% end %>


                          The same approach can be used with straight ERB inpolation if you just join an array of classes:



                          <div class="<%= classes.join(" ")%>" ...





                          share|improve this answer












                          In Ruby you can use Enumerable#each_with_index to iterate through a collection with an index:



                          <% @question.each_with_index do |value, index| %>

                          <% end %>

                          <% end %>


                          In general in Ruby if you are using .each or any iterator with an external mutating variable you are doing it wrong.



                          You can also clean up the view by using content_tag:



                          <% @question.each_with_index do |value, index| %>
                          <%
                          classes = ["tab-pane", "fade"]
                          classes = classes + ["active", "show"] if index == 0
                          %>
                          <%= content_tag :div, class: classes, id: "question#{index+1}" do %>

                          <% end %>
                          <% end %>


                          The same approach can be used with straight ERB inpolation if you just join an array of classes:



                          <div class="<%= classes.join(" ")%>" ...






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          max

                          43.4k856103




                          43.4k856103






















                              Danz Tim is a new contributor. Be nice, and check out our Code of Conduct.










                               

                              draft saved


                              draft discarded


















                              Danz Tim is a new contributor. Be nice, and check out our Code of Conduct.













                              Danz Tim is a new contributor. Be nice, and check out our Code of Conduct.












                              Danz Tim is a new contributor. Be nice, and check out our Code of Conduct.















                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238178%2fclass-name-in-html-erb-file-separated%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              Popular posts from this blog

                              Full-time equivalent

                              さくらももこ

                              13 indicted, 8 arrested in Calif. drug cartel investigation