Guest User

Untitled

a guest
Jan 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. <p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></strike></p>
  2.  
  3. <p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide_item.image %></strike></p>
  4.  
  5. class BuyingGuidesController < ApplicationController
  6. before_action :set_buying_guide, only: [:show, :edit, :update, :destroy]
  7.  
  8. # GET /buying_guides
  9. # GET /buying_guides.json
  10. def index
  11. @buying_guides = BuyingGuide.all
  12. end
  13.  
  14. # GET /buying_guides/1
  15. # GET /buying_guides/1.json
  16. def show
  17. end
  18.  
  19. # GET /buying_guides/new
  20. def new
  21. @buying_guide = BuyingGuide.new
  22. end
  23.  
  24. # GET /buying_guides/1/edit
  25. def edit
  26. end
  27.  
  28. # POST /buying_guides
  29. # POST /buying_guides.json
  30. def create
  31. @buying_guide = BuyingGuide.new(buying_guide_params)
  32.  
  33. respond_to do |format|
  34. if @buying_guide.save
  35. format.html { redirect_to @buying_guide, notice: 'Buying guide was successfully created.' }
  36. format.json { render :show, status: :created, location: @buying_guide }
  37. else
  38. format.html { render :new }
  39. format.json { render json: @buying_guide.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43.  
  44. # PATCH/PUT /buying_guides/1
  45. # PATCH/PUT /buying_guides/1.json
  46. def update
  47. respond_to do |format|
  48. if @buying_guide.update(buying_guide_params)
  49. format.html { redirect_to @buying_guide, notice: 'Buying guide was successfully updated.' }
  50. format.json { render :show, status: :ok, location: @buying_guide }
  51. else
  52. format.html { render :edit }
  53. format.json { render json: @buying_guide.errors, status: :unprocessable_entity }
  54. end
  55. end
  56. end
  57.  
  58. # DELETE /buying_guides/1
  59. # DELETE /buying_guides/1.json
  60. def destroy
  61. @buying_guide.destroy
  62. respond_to do |format|
  63. format.html { redirect_to buying_guides_url, notice: 'Buying guide was successfully destroyed.' }
  64. format.json { head :no_content }
  65. end
  66. end
  67.  
  68. private
  69. # Use callbacks to share common setup or constraints between actions.
  70. def set_buying_guide
  71. @buying_guide = BuyingGuide.find(params[:id])
  72. end
  73.  
  74. # Never trust parameters from the scary internet, only allow the white list through.
  75. def buying_guide_params
  76. params.require(:buying_guide).permit(:title, :description, :image)
  77. end
  78.  
  79. end
  80.  
  81. class BuyingGuideItemsController < ApplicationController
  82.  
  83. before_action :set_buying_guide
  84. before_action :set_buying_guide_item, except: [:create]
  85.  
  86. def create
  87. @buying_guide_item = @buying_guide.buying_guide_items.create(buying_guide_item_params)
  88. redirect_to @buying_guide
  89. end
  90.  
  91. def destroy
  92. @buying_guide_item = @buying_guide.buying_guide_items.find(params[:id])
  93. if @buying_guide_item.destroy
  94. flash[:success] = "Buying guide list item was deleted."
  95. else
  96. flash[:error] = "Buying guide list item could not be deleted."
  97. end
  98. redirect_to @buying_guide
  99. end
  100.  
  101. def complete
  102. @buying_guide_item.update_attribute(:completed_at, Time.now)
  103. redirect_to @buying_guide, notice: "Buying guide item completed."
  104. end
  105.  
  106. private
  107.  
  108. def set_buying_guide
  109. @buying_guide = BuyingGuide.find(params[:buying_guide_id])
  110. end
  111.  
  112. def set_buying_guide_item
  113. @buying_guide_item = @buying_guide.buying_guide_items.find(params[:id])
  114. end
  115.  
  116. def buying_guide_item_params
  117. params[:buying_guide_item].permit(:content, :image)
  118. end
  119.  
  120. end
  121.  
  122. class BuyingGuide < ApplicationRecord
  123. has_many :buying_guide_items, :dependent => :destroy
  124. has_one_attached :image
  125. end
  126.  
  127. class BuyingGuideItem < ApplicationRecord
  128. belongs_to :buying_guide
  129.  
  130. def completed?
  131. !completed_at.blank?
  132. end
  133.  
  134. has_one_attached :image
  135.  
  136. end
  137.  
  138. <div class="row clearfix">
  139. <% if buying_guide_item.completed? %>
  140.  
  141. <div class="complete">
  142. <%= link_to complete_buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id), method:
  143. :patch do %>
  144. <i style="opacity: 0.4;" class="fa fa-check"></i>
  145. <% end %>
  146. </div>
  147.  
  148. <div class="item">
  149. <p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></strike></p>
  150. </div>
  151.  
  152. <div class="trash">
  153. <%= link_to buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id),
  154. method: :delete, data: { confirm: "Are you sure?" } do %>
  155. <i class="fa fa-trash"></i>
  156. <% end %>
  157. </div>
  158.  
  159. <% else %>
  160.  
  161. <div class="complete">
  162. <%= link_to complete_buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id), method:
  163. :patch do %>
  164. <i class="fa fa-circle-thin"></i>
  165. <% end %>
  166. </div>
  167.  
  168. <div class="item">
  169. <p><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></p>
  170. </div>
  171. <div class="trash">
  172. <%= link_to buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id),
  173. method: :delete, data: { confirm: "Are you sure?" } do %>
  174. <i class="fa fa-trash"></i>
  175. <% end %>
  176. </div>
  177.  
  178. <% end %>
  179. </div>
  180.  
  181. <%= form_for([@buying_guide, @buying_guide.buying_guide_items.build]) do |f| %>
  182. <br>
  183. <%= f.text_field :content, placeholder: "..." %>
  184. <%= f.file_field :image %>
  185. <%= f.submit :"Submit" %>
  186. <% end %>
  187.  
  188. <%= form_with(model: buying_guide, local: true) do |form| %>
  189. <% if buying_guide.errors.any? %>
  190. <div id="error_explanation">
  191. <h2><%= pluralize(buying_guide.errors.count, "error") %> prohibited this buying_guide from being saved:</h2>
  192.  
  193. <ul>
  194. <% buying_guide.errors.full_messages.each do |message| %>
  195. <li><%= message %></li>
  196. <% end %>
  197. </ul>
  198. </div>
  199. <% end %>
  200.  
  201. <div class="field">
  202. <%= form.label :title %>
  203. <%= form.text_field :title %>
  204. </div>
  205.  
  206. <div class="field">
  207. <%= form.label :description %>
  208. <%= form.text_area :description %>
  209. </div>
  210.  
  211. <div>
  212. <%= form.label :image %>
  213. <%= form.file_field :image %>
  214. </div>
  215.  
  216. <div>
  217. <%= form.submit :"Submit" %>
  218. </div>
  219. <% end %>
  220.  
  221. <p id="notice"><%= notice %></p>
  222.  
  223. <h2 class="list_title"><%= @buying_guide.title %></h2>
  224.  
  225. <div id="items_wrapper">
  226. <%= render @buying_guide.buying_guide_items %>
  227. <%= image_tag @buying_guide.image %>
  228. <div id="form">
  229. <%= render "buying_guide_items/form" %>
  230. </div>
  231. </div>
  232.  
  233. <div class="links">
  234. <%= link_to 'Back', buying_guides_path %> |
  235. <%= link_to 'Edit', edit_buying_guide_path(@buying_guide) %> |
  236. <%= link_to 'Delete', buying_guide_path(@buying_guide), method: :delete, data:
  237. { confirm: "Are you sure?" } %>
  238. </div>
  239.  
  240. class CreateBuyingGuideItems < ActiveRecord::Migration[5.2]
  241.  
  242. def change
  243. create_table :buying_guide_items do |t|
  244. t.string :content
  245. t.references :buying_guide, foreign_key: true
  246.  
  247. t.timestamps
  248. end
  249. end
  250. end
  251.  
  252. Rails.application.routes.draw do
  253. get 'welcome/index'
  254.  
  255. resources :buying_guides do
  256. resources :buying_guide_items do
  257. member do
  258. patch :complete
  259. end
  260. end
  261. end
Add Comment
Please, Sign In to add comment