Always generate <button> tags with button_to

In Rails 6.1 and earlier, `button_to` generated a <button> tag when it
received the content as a block, but an <input> tag when receiving
the content as the first parameter.

That's why we were using blocks with `button_to` most of the time; for
starters, <button> tags accept pseudocontent and so are easier to style.

In Rails 7.0, `button_to` always generates a <button> tag [1], so we're
simplifying the code what uses `button_to`, passing the content as a
first parameter instead of passing it as a block.

[1] https://guides.rubyonrails.org/v7.1/configuring.html#config-action-view-button-to-generates-button-tag
This commit is contained in:
Javi Martín
2024-03-27 23:53:39 +01:00
parent 8596f1539f
commit f384451d9f
6 changed files with 20 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
<% if button? %> <% if button? %>
<%= button_to(path, html_options) { text } %> <%= button_to text, path, html_options %>
<% else %> <% else %>
<%= link_to text, path, html_options %> <%= link_to text, path, html_options %>
<% end %> <% end %>

View File

@@ -10,15 +10,14 @@
</p> </p>
<% end %> <% end %>
<% if investment.should_show_ballots? %> <% if investment.should_show_ballots? %>
<%= button_to budget_ballot_line_path(id: investment.id, <%= button_to t("budgets.ballots.show.remove"),
budget_ballot_line_path(id: investment.id,
budget_id: investment.budget_id, budget_id: investment.budget_id,
investments_ids: investment_ids), investments_ids: investment_ids),
class: "button button-remove-support expanded", class: "button button-remove-support expanded",
method: :delete, method: :delete,
remote: true, remote: true,
"aria-label": remove_vote_aria_label do %> "aria-label": remove_vote_aria_label %>
<%= t("budgets.ballots.show.remove") %>
<% end %>
<% end %> <% end %>
</div> </div>
<% else %> <% else %>
@@ -29,16 +28,15 @@
</p> </p>
<% end %> <% end %>
<% if investment.should_show_ballots? %> <% if investment.should_show_ballots? %>
<%= button_to budget_ballot_lines_path(investment_id: investment.id, <%= button_to t("budgets.investments.investment.add"),
budget_ballot_lines_path(investment_id: investment.id,
budget_id: investment.budget_id, budget_id: investment.budget_id,
investments_ids: investment_ids), investments_ids: investment_ids),
class: "button button-support expanded", class: "button button-support expanded",
title: t("budgets.investments.investment.support_title"), title: t("budgets.investments.investment.support_title"),
method: :post, method: :post,
remote: true, remote: true,
"aria-label": vote_aria_label do %> "aria-label": vote_aria_label %>
<%= t("budgets.investments.investment.add") %>
<% end %>
<% end %> <% end %>
</div> </div>
<% end %> <% end %>

View File

@@ -12,13 +12,12 @@
<%= t("budgets.investments.votes.already_supported") %> <%= t("budgets.investments.votes.already_supported") %>
</div> </div>
<% if feature?(:remove_investments_supports) %> <% if feature?(:remove_investments_supports) %>
<%= button_to remove_support_path, <%= button_to t("budgets.investments.votes.remove_support"),
remove_support_path,
class: "button button-remove-support expanded", class: "button button-remove-support expanded",
method: "delete", method: "delete",
remote: true, remote: true,
"aria-label": remove_support_aria_label do %> "aria-label": remove_support_aria_label %>
<%= t("budgets.investments.votes.remove_support") %>
<% end %>
<% end %> <% end %>
</div> </div>
<% else %> <% else %>

View File

@@ -2,23 +2,21 @@
<% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> <% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %>
<% question_answers.each do |question_answer| %> <% question_answers.each do |question_answer| %>
<% if already_answered?(question_answer) %> <% if already_answered?(question_answer) %>
<%= button_to question_answer_path(question, user_answer(question_answer)), <%= button_to question_answer.title,
question_answer_path(question, user_answer(question_answer)),
method: :delete, method: :delete,
remote: true, remote: true,
title: t("poll_questions.show.voted", answer: question_answer.title), title: t("poll_questions.show.voted", answer: question_answer.title),
class: "button answered", class: "button answered",
"aria-pressed": true do %> "aria-pressed": true %>
<%= question_answer.title %>
<% end %>
<% else %> <% else %>
<%= button_to answer_question_path(question, answer: question_answer.title), <%= button_to question_answer.title,
answer_question_path(question, answer: question_answer.title),
remote: true, remote: true,
title: t("poll_questions.show.vote_answer", answer: question_answer.title), title: t("poll_questions.show.vote_answer", answer: question_answer.title),
class: "button secondary hollow", class: "button secondary hollow",
"aria-pressed": false, "aria-pressed": false,
disabled: disable_answer?(question_answer) do %> disabled: disable_answer?(question_answer) %>
<%= question_answer.title %>
<% end %>
<% end %> <% end %>
<% end %> <% end %>
<% elsif !user_signed_in? %> <% elsif !user_signed_in? %>

View File

@@ -7,14 +7,13 @@
<%= t("proposals.proposal.already_supported") %> <%= t("proposals.proposal.already_supported") %>
</div> </div>
<% else %> <% else %>
<%= button_to vote_url, <%= button_to t("proposals.proposal.support"),
vote_url,
class: "button button-support small expanded", class: "button button-support small expanded",
title: t("proposals.proposal.support_title"), title: t("proposals.proposal.support_title"),
method: "post", method: "post",
remote: true, remote: true,
"aria-label": support_aria_label do %> "aria-label": support_aria_label %>
<%= t("proposals.proposal.support") %>
<% end %>
<% end %> <% end %>
</div> </div>

View File

@@ -11,7 +11,7 @@
# `button_to` view helper will render `<button>` element, regardless of whether # `button_to` view helper will render `<button>` element, regardless of whether
# or not the content is passed as the first argument or as a block. # or not the content is passed as the first argument or as a block.
# Rails.application.config.action_view.button_to_generates_button_tag = true Rails.application.config.action_view.button_to_generates_button_tag = true
# `stylesheet_link_tag` view helper will not render the media attribute by default. # `stylesheet_link_tag` view helper will not render the media attribute by default.
# Rails.application.config.action_view.apply_stylesheet_media_default = false # Rails.application.config.action_view.apply_stylesheet_media_default = false