Ruby rails: view helpers: Difference between revisions
From wikinotes
(→forms) |
|||
Line 49: | Line 49: | ||
== forms == | == forms == | ||
<blockquote> | <blockquote> | ||
form for an action | |||
<source lang="ruby"> | <source lang="ruby"> | ||
<%= form_tag(action: "post") do %> | <%= form_tag(action: "post") do %> | ||
<%= select_tag("users", options_for_select(["Alex", "Courtney", "Sam"], selected: "Courtney")) %> | <%= select_tag("users", options_for_select(["Alex", "Courtney", "Sam"], selected: "Courtney")) %> | ||
<%= submit_tag %> | <%= submit_tag %> | ||
<!-- ... --> | |||
<% end %> | <% end %> | ||
</source> | </source> | ||
form for creating an ActiveRecord model (id: <code>"new_person"</code>) | |||
<syntaxhighlight lang="ruby"> | |||
<%= form_for(@person) do %> | |||
<%= select_tag("friends", options_for_select(["Alex", "Courtney", "Sam"], selected: "Alex")) %> | |||
<!-- ... --> | |||
<% end %> | |||
</syntaxhighlight> | |||
=== button_tag === | === button_tag === | ||
<blockquote> | |||
button with icon | button with icon | ||
<source lang="erb"> | <source lang="erb"> | ||
Line 66: | Line 74: | ||
<% end %> | <% end %> | ||
</source> | </source> | ||
</blockquote><!-- button_tag --> | |||
=== select_tag === | === select_tag === | ||
<blockquote> | |||
* select https://api.rubyonrails.org/files/actionview/lib/action_view/helpers/form_options_helper_rb.html | * select https://api.rubyonrails.org/files/actionview/lib/action_view/helpers/form_options_helper_rb.html | ||
* select_tag https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag | * select_tag https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag | ||
Line 88: | Line 98: | ||
<% select("user", "id", [["alex", 1], ["courtney", 2], ["sam", 3]]) %> | <% select("user", "id", [["alex", 1], ["courtney", 2], ["sam", 3]]) %> | ||
</source> | </source> | ||
</blockquote><!-- select_tag --> | |||
=== submit_tag === | === submit_tag === | ||
<blockquote> | |||
</blockquote><!-- submit_tag --> | |||
</blockquote><!-- forms --> | </blockquote><!-- forms --> | ||
</blockquote><!-- common helpers --> | </blockquote><!-- common helpers --> |
Revision as of 18:54, 1 September 2021
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/aredirect_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 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 %>button_tag
button with icon
<%= button_tag(type: "submit") do %> <i class=" icon-repeat"></i> <% end %>select_tag
- select https://api.rubyonrails.org/files/actionview/lib/action_view/helpers/form_options_helper_rb.html
- select_tag https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-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