Ruby on Rails: Increase value of object in scaffold with button_to
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
add a comment |
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
add a comment |
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
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
html ruby-on-rails methods button-to
asked Nov 25 '18 at 13:36
user9384572user9384572
133
133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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:
- Clutter your controller too much
- Don't handle connection issues/reloading too well (think if you have
0losses, clickadd, something goes wrong with rendering the response, the counter has been updated to1, but the user goes back to the previous page, sees0again and clicksaddagain, you'd end up on2instead of1.
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- Clutter your controller too much
- Don't handle connection issues/reloading too well (think if you have
0losses, clickadd, something goes wrong with rendering the response, the counter has been updated to1, but the user goes back to the previous page, sees0again and clicksaddagain, you'd end up on2instead of1.
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.
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
add a comment |
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:
- Clutter your controller too much
- Don't handle connection issues/reloading too well (think if you have
0losses, clickadd, something goes wrong with rendering the response, the counter has been updated to1, but the user goes back to the previous page, sees0again and clicksaddagain, you'd end up on2instead of1.
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.
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
add a comment |
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:
- Clutter your controller too much
- Don't handle connection issues/reloading too well (think if you have
0losses, clickadd, something goes wrong with rendering the response, the counter has been updated to1, but the user goes back to the previous page, sees0again and clicksaddagain, you'd end up on2instead of1.
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.
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:
- Clutter your controller too much
- Don't handle connection issues/reloading too well (think if you have
0losses, clickadd, something goes wrong with rendering the response, the counter has been updated to1, but the user goes back to the previous page, sees0again and clicksaddagain, you'd end up on2instead of1.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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