Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Модель Item
- class Item < ApplicationRecord
- has_attached_file :image, styles: { small: '64x64', med: '100x100', large: '200x200' }
- validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
- end
- # migrate
- class AddImageToItems < ActiveRecord::Migration[5.0]
- def self.up
- change_table :items do |t|
- t.attachment :image
- end
- end
- def self.down
- remove_attachment :items, :image
- end
- end
- # edit.html.erb
- <h1>Edit new item</h1>
- <%= form_for @item, html: { multipart: true } do |f| %>
- <%= render partial: "form", locals: { f: f } %>
- <p></br><%= f.submit "Обновить товар"%></p>
- <% end %>
- # new.html.erb
- <h1>Create new item</h1>
- <%= form_for @item, html: { multipart: true } do |f| %>
- <%= render partial: "form", locals: { f: f } %>
- <p></br><%= f.submit "Создать товар"%></p>
- <% end %>
- #index.html.erb
- <h1>Items</h1>
- <% if @items.empty? %>
- <b>No items in store</b>
- <% else %>
- <table>
- <tr>
- <th>item id</th>
- <th>Name</th>
- <th>Price</th>
- <th>Описания</th>
- <th>Дата создания товара</th>
- <th></th>
- <th>Рейтинг</th>
- <th>Image</th>
- </tr>
- <% @items.each do |i| %>
- <tr>
- <td><%= i.id %></td>
- <td><%= link_to i.name, item_path(i) %></td>
- <td><%= i.price %> руб.</td>
- <td><%= i.description %></td>
- <td><%= i.created_at.to_s.sub("2016", "2017") %></td>
- <td><%= link_to "Удалить", i, method: :delete, data: {confirm: "Точно удалить?" }%> </td>
- <td>Рейтинг: <%= i.votes_count %>, <%= link_to "+", upvote_item_path(i) %></td>
- <td><%= image_tag i.image.url(:med) %></td>
- </tr>
- <% end %>
- </table>
- <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement