Ruby rails: view helpers

From wikinotes

Documentation

View Helpers https://api.rubyonrails.org/classes/ActionView/Helpers.html
Specific Helpers
formhelper https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
urlhelper

Common Helpers

Url Helpers

See ruby rails: url helpers for details.

a_rails_route_path(...)  # rails_route/a
a_rails_route_url(...)   # https://domain.com/rails_route/a

redirect_back

Redirect back to the page you came to this one from.

redirect_back(
  fallback_location: ..,
  flash: { notice: "completed blah" }
)

Elements

link_to

button_to

button_to("Go Here", some_rails_path(123))

forms

form types

form for an action

<%= form_tag(action: "post") do %>
  <%= select_tag("users", options_for_select(["Alex", "Courtney", "Sam"], selected: "Courtney")) %>
  <%= submit_tag %>
  <!-- ... -->
<% end %>

form for creating an ActiveRecord model (id: "new_person")

<%= form_for(@person) do %>
  <%= select_tag("friends", options_for_select(["Alex", "Courtney", "Sam"], selected: "Alex")) %>
  <!-- ... -->
<% end %>

form fields

button_tag

button with icon

<%= button_tag(type: "submit") do %>
  <i class=" icon-repeat"></i>
<% end %>

select_tag

<!-- select with label/value -->
<!-- params={"number"=>2} -->
<%= select_tag("number", options_for_select({ "a" => 1, "b" => 2})) %>
<!-- select from collection -->
<!-- params={"letter"=>"b"} -->
<%= select_tag("letter", options_for_select(["a", "b", "c"], selected: "b")) %>
<!-- show "Alex", save 1 -->
<!-- params={"user"=>{ "id"=> 2}} -->
<% select("user", "id", [["alex", 1], ["courtney", 2], ["sam", 3]]) %>

submit_tag