Tag: Rails/ビューヘルパー
<%= form_for(@book) do |f| %> タイトル: <%= f.text_field(:title, {:size => 40}) %> <%= f.submit %> <%= end %>
<%= form_tag({ :controller => 'books', :action => 'create'}) do %> タイトル: <%= text_field(:book , :title, {:size => 40}) %> <%= f.submit %> <% end %>
<%= f.label :title, 'タイトル' %>
tag系とそれ以外で最後のオプションの渡し方がことなるので注意。
<%= f.select(:object_field, ['Item 1', ...], {}, { class: 'my_style_class' }) %>
<%= f.select(:category_id, Category.all.map{|category| [category.title, category.id]}, {include_blank: '選択'}, {class: 'form-control'} ) %>
select_tag 'xxx', options_for_select(@objects.map{|obj| [obj.title, obj.id]})
<%= submit_tag('削除', {name: 'delete', class: 'btn btn-danger', data: {confirm: '削除しますか?'} } ) %>
def bootstrap_class_for flash_type { success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type.to_sym] || flash_type.to_s end def flash_messages(opts = {}) flash.each do |msg_type, message| concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} alert-dismissible", role: 'alert') do concat(content_tag(:button, class: 'close', data: { dismiss: 'alert' }) do concat content_tag(:span, '×'.html_safe, 'aria-hidden' => true) concat content_tag(:span, 'Close', class: 'sr-only') end) concat message end) end nil
<div class="container"> <!-- flash message --> <%= flash_messages %> <%= yield %> <hr> <footer> <p>© src256</p> </footer> </div>
msg = "保存しました" flash[:notice] = msg redirect_to(software_path(id, take_params))
<h1>アイテム一覧</h1> <table class="table"> <tr> <th class="col-xs-3">カテゴリ</th> <th class="col-xs-3">タイトル</th> <th class="col-xs-3">コンテンツ</th> <th class="col-xs-3">操作</th> </tr> <% @items.each do |item| %> <tr> <td><%= item.category_id %></td> <td><%= item.title %></td> <td><%= item.content %></td> <td><%= link_to 'Show', item %></td> <td><%= link_to 'Edit', edit_item_path(item) %></td> <td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </table> <br> <%= link_to 'アイテム追加', new_item_path %>
<%= form_for(@item, html: {class: 'form-horizontal'}) do |f| %> <% if @item.errors.any? %> <div id="error_explanation"> <h2>エラーが発生しました :</h2> <ul> <% @item.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="form-group"> <%= f.label(:category_id, 'カテゴリ', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.select(:category_id, Category.all.map{|category| [category.title, category.id]}, {include_blank: '選択'}, {class: 'form-control'} ) %> </div> </div> <div class="form-group"> <%= f.label(:folder_id, 'フォルダ', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.select(:folder_id, Folder.all.map{|folder| [folder.title, folder.id]}, {include_blank: '選択'}, {class: 'form-control'} ) %> </div> </div> <div class="form-group"> <%= f.label(:title, 'タイトル', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.text_field(:title, class: 'form-control') %> </div> </div> <div class="form-group"> <%= f.label(:content, 'コンテンツ', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.text_area(:content, rows: 14, cols: 20, class: 'form-control') %> </div> </div> <div class="form-group"> <div class="col-xs-12"> <%= f.submit('保存', class: 'btn btn-primary') %> </div> </div> <% end %>