Ruby on Rails: Increase value of object in scaffold with button_to












2















I have a scaffold that generates a table of basketball teams. I want to implement a button in the table that increases the number of wins for a team and another for the losses.By default, both are set to 0 in the controller. Should I implement a method in the controller or the view (add_wins and add_losses)? If so, how would it look like? Thank You.
This is the code for the body of the table in the view:



<tbody>
<% @teams.each do |team| %>
<tr>
<td><%= team.Name %></td>
<td><%= team.Color %></td>
<td><%= team.Players %></td>
<td><%= team.Wins %></td>
<td style="font-size: 20px"><%= button_to '+', method: :add_wins %></td> <!-- The method add_wins doesn't exist. -->
<td><%= team.Losses %></td>
<td style="font-size: 20px"><%= button_to '+',method: :add_losses %></td> <!-- The method add_losses doesn't exist. -->
<td><%= link_to 'Show', team %></td>
<td><%= link_to 'Edit', edit_team_path(team) %></td>
<td><%= link_to 'Delete', team, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>












share|improve this question



























    2















    I have a scaffold that generates a table of basketball teams. I want to implement a button in the table that increases the number of wins for a team and another for the losses.By default, both are set to 0 in the controller. Should I implement a method in the controller or the view (add_wins and add_losses)? If so, how would it look like? Thank You.
    This is the code for the body of the table in the view:



    <tbody>
    <% @teams.each do |team| %>
    <tr>
    <td><%= team.Name %></td>
    <td><%= team.Color %></td>
    <td><%= team.Players %></td>
    <td><%= team.Wins %></td>
    <td style="font-size: 20px"><%= button_to '+', method: :add_wins %></td> <!-- The method add_wins doesn't exist. -->
    <td><%= team.Losses %></td>
    <td style="font-size: 20px"><%= button_to '+',method: :add_losses %></td> <!-- The method add_losses doesn't exist. -->
    <td><%= link_to 'Show', team %></td>
    <td><%= link_to 'Edit', edit_team_path(team) %></td>
    <td><%= link_to 'Delete', team, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
    <% end %>












    share|improve this question

























      2












      2








      2








      I have a scaffold that generates a table of basketball teams. I want to implement a button in the table that increases the number of wins for a team and another for the losses.By default, both are set to 0 in the controller. Should I implement a method in the controller or the view (add_wins and add_losses)? If so, how would it look like? Thank You.
      This is the code for the body of the table in the view:



      <tbody>
      <% @teams.each do |team| %>
      <tr>
      <td><%= team.Name %></td>
      <td><%= team.Color %></td>
      <td><%= team.Players %></td>
      <td><%= team.Wins %></td>
      <td style="font-size: 20px"><%= button_to '+', method: :add_wins %></td> <!-- The method add_wins doesn't exist. -->
      <td><%= team.Losses %></td>
      <td style="font-size: 20px"><%= button_to '+',method: :add_losses %></td> <!-- The method add_losses doesn't exist. -->
      <td><%= link_to 'Show', team %></td>
      <td><%= link_to 'Edit', edit_team_path(team) %></td>
      <td><%= link_to 'Delete', team, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
      <% end %>












      share|improve this question














      I have a scaffold that generates a table of basketball teams. I want to implement a button in the table that increases the number of wins for a team and another for the losses.By default, both are set to 0 in the controller. Should I implement a method in the controller or the view (add_wins and add_losses)? If so, how would it look like? Thank You.
      This is the code for the body of the table in the view:



      <tbody>
      <% @teams.each do |team| %>
      <tr>
      <td><%= team.Name %></td>
      <td><%= team.Color %></td>
      <td><%= team.Players %></td>
      <td><%= team.Wins %></td>
      <td style="font-size: 20px"><%= button_to '+', method: :add_wins %></td> <!-- The method add_wins doesn't exist. -->
      <td><%= team.Losses %></td>
      <td style="font-size: 20px"><%= button_to '+',method: :add_losses %></td> <!-- The method add_losses doesn't exist. -->
      <td><%= link_to 'Show', team %></td>
      <td><%= link_to 'Edit', edit_team_path(team) %></td>
      <td><%= link_to 'Delete', team, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
      <% end %>









      html ruby-on-rails methods button-to






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 13:36









      user9384572user9384572

      133




      133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Not a fully copy-paste answer, but I'll try to explain the basics, so hopefully you can figure out the whole solution on your own.



          First of all, I'd advise you against implementing separate add_wins, add_losses methods as these:




          1. Clutter your controller too much

          2. Don't handle connection issues/reloading too well (think if you have 0 losses, click add, something goes wrong with rendering the response, the counter has been updated to 1, but the user goes back to the previous page, sees 0 again and clicks add again, you'd end up on 2 instead of 1.


          Instead, stick to the standard update action with proper parameters.



          Here are the docs for button_to, you will have to set proper method and params. Your params need to contain the next value for losses or wins and the team id. Additionally, you may wish to validate that somebody is not trying to send some arbitrary value for wins or losses. The method you'll want to use is put.






          share|improve this answer
























          • Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

            – user9384572
            Nov 26 '18 at 20:24













          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%2f53468019%2fruby-on-rails-increase-value-of-object-in-scaffold-with-button-to%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









          0














          Not a fully copy-paste answer, but I'll try to explain the basics, so hopefully you can figure out the whole solution on your own.



          First of all, I'd advise you against implementing separate add_wins, add_losses methods as these:




          1. Clutter your controller too much

          2. Don't handle connection issues/reloading too well (think if you have 0 losses, click add, something goes wrong with rendering the response, the counter has been updated to 1, but the user goes back to the previous page, sees 0 again and clicks add again, you'd end up on 2 instead of 1.


          Instead, stick to the standard update action with proper parameters.



          Here are the docs for button_to, you will have to set proper method and params. Your params need to contain the next value for losses or wins and the team id. Additionally, you may wish to validate that somebody is not trying to send some arbitrary value for wins or losses. The method you'll want to use is put.






          share|improve this answer
























          • Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

            – user9384572
            Nov 26 '18 at 20:24


















          0














          Not a fully copy-paste answer, but I'll try to explain the basics, so hopefully you can figure out the whole solution on your own.



          First of all, I'd advise you against implementing separate add_wins, add_losses methods as these:




          1. Clutter your controller too much

          2. Don't handle connection issues/reloading too well (think if you have 0 losses, click add, something goes wrong with rendering the response, the counter has been updated to 1, but the user goes back to the previous page, sees 0 again and clicks add again, you'd end up on 2 instead of 1.


          Instead, stick to the standard update action with proper parameters.



          Here are the docs for button_to, you will have to set proper method and params. Your params need to contain the next value for losses or wins and the team id. Additionally, you may wish to validate that somebody is not trying to send some arbitrary value for wins or losses. The method you'll want to use is put.






          share|improve this answer
























          • Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

            – user9384572
            Nov 26 '18 at 20:24
















          0












          0








          0







          Not a fully copy-paste answer, but I'll try to explain the basics, so hopefully you can figure out the whole solution on your own.



          First of all, I'd advise you against implementing separate add_wins, add_losses methods as these:




          1. Clutter your controller too much

          2. Don't handle connection issues/reloading too well (think if you have 0 losses, click add, something goes wrong with rendering the response, the counter has been updated to 1, but the user goes back to the previous page, sees 0 again and clicks add again, you'd end up on 2 instead of 1.


          Instead, stick to the standard update action with proper parameters.



          Here are the docs for button_to, you will have to set proper method and params. Your params need to contain the next value for losses or wins and the team id. Additionally, you may wish to validate that somebody is not trying to send some arbitrary value for wins or losses. The method you'll want to use is put.






          share|improve this answer













          Not a fully copy-paste answer, but I'll try to explain the basics, so hopefully you can figure out the whole solution on your own.



          First of all, I'd advise you against implementing separate add_wins, add_losses methods as these:




          1. Clutter your controller too much

          2. Don't handle connection issues/reloading too well (think if you have 0 losses, click add, something goes wrong with rendering the response, the counter has been updated to 1, but the user goes back to the previous page, sees 0 again and clicks add again, you'd end up on 2 instead of 1.


          Instead, stick to the standard update action with proper parameters.



          Here are the docs for button_to, you will have to set proper method and params. Your params need to contain the next value for losses or wins and the team id. Additionally, you may wish to validate that somebody is not trying to send some arbitrary value for wins or losses. The method you'll want to use is put.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 13:54









          Marcin KołodziejMarcin Kołodziej

          4,5001315




          4,5001315













          • Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

            – user9384572
            Nov 26 '18 at 20:24





















          • Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

            – user9384572
            Nov 26 '18 at 20:24



















          Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

          – user9384572
          Nov 26 '18 at 20:24







          Thank You! It worked. My buttons look like this now: <%= button_to "+", team, method: :put, params: { :id => team.id, :Wins => team.Wins + 1 } %>

          – user9384572
          Nov 26 '18 at 20:24






















          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%2f53468019%2fruby-on-rails-increase-value-of-object-in-scaffold-with-button-to%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

          Ottavio Pratesi

          Tricia Helfer

          15 giugno