Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.81 KB | None | 0 0
  1. # Модель Item
  2. class Item < ApplicationRecord
  3.  
  4.   has_attached_file :image, styles: { small: '64x64', med: '100x100', large: '200x200' }
  5.   validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
  6. end
  7.  
  8. # migrate
  9. class AddImageToItems < ActiveRecord::Migration[5.0]
  10.   def self.up
  11.     change_table :items do |t|
  12.       t.attachment :image
  13.     end
  14.   end
  15.  
  16.   def self.down
  17.     remove_attachment :items, :image
  18.   end
  19. end
  20.  
  21. # edit.html.erb
  22. <h1>Edit new item</h1>
  23.  
  24. <%= form_for @item, html: { multipart: true } do |f|  %>
  25.   <%= render partial: "form", locals: { f: f } %>
  26.   <p></br><%= f.submit "Обновить товар"%></p>
  27. <% end %>
  28.  
  29. # new.html.erb
  30. <h1>Create new item</h1>
  31.  
  32. <%= form_for @item, html: { multipart: true } do |f|  %>
  33.   <%= render partial: "form", locals: { f: f } %>
  34.   <p></br><%= f.submit "Создать товар"%></p>
  35. <% end %>
  36.  
  37. #index.html.erb
  38. <h1>Items</h1>
  39.  
  40. <% if @items.empty? %>
  41.   <b>No items in store</b>
  42. <% else %>
  43.   <table>
  44.     <tr>
  45.       <th>item id</th>
  46.       <th>Name</th>
  47.       <th>Price</th>
  48.       <th>Описания</th>
  49.       <th>Дата создания товара</th>
  50.       <th></th>
  51.       <th>Рейтинг</th>
  52.       <th>Image</th>
  53.     </tr>
  54.  
  55.     <% @items.each do |i| %>
  56.       <tr>
  57.         <td><%= i.id %></td>
  58.         <td><%= link_to i.name, item_path(i) %></td>
  59.         <td><%= i.price %> руб.</td>
  60.         <td><%= i.description %></td>
  61.         <td><%= i.created_at.to_s.sub("2016", "2017") %></td>
  62.         <td><%= link_to "Удалить", i, method: :delete, data: {confirm: "Точно удалить?" }%> </td>
  63.         <td>Рейтинг: <%= i.votes_count %>, <%= link_to "+", upvote_item_path(i) %></td>
  64.         <td><%= image_tag  i.image.url(:med) %></td>
  65.       </tr>
  66.     <% end %>
  67.   </table>
  68. <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement