Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. .container
  2. .row
  3. h1.text-gray Tutorials
  4. .row.search_banner
  5. .col-xs-3
  6. =form_tag tutos_path, :method => 'get' do
  7. =text_field_tag :search, params[:search], placeholder:"Search by keywords"
  8. =submit_tag "Search", class:'btn btn-xs btn-default btn-search'
  9.  
  10.  
  11. ###THE PROBLEM IS STARTD HERE ###
  12.  
  13. .col-xs-3
  14. =form_tag tutos_path, :method => 'get' do
  15. =select_tag :filter, options_for_select(Category.all.map{|c| c.name}, params[:filter])
  16. =submit_tag "Search", class:"btn btn-xs btn-default btn-search"
  17.  
  18. .col-xs-3
  19. -if user_signed_in?
  20. = link_to "Create a tuto", new_tuto_path, class:"btn btn-success"
  21. br
  22. br
  23.  
  24.  
  25. #tutos.transitions-enabled
  26. -@tutos.each do |tuto|
  27. .box.panel-default
  28. = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto)
  29.  
  30.  
  31. h3 = link_to tuto.title, tuto_path(tuto), class:"title-link"
  32. h6
  33. | Created by:
  34. span<>
  35. = tuto.user.full_name
  36. br
  37. span.glyphicon.glyphicon-heart
  38. span<>
  39. = tuto.get_upvotes.size
  40.  
  41. class Tuto < ActiveRecord::Base
  42. acts_as_votable
  43. belongs_to :user
  44. belongs_to :category
  45. validates :category_id, presence: true
  46.  
  47. def self.search(search)
  48. if search
  49. where(["title LIKE ?","%#{search}%"])
  50. else
  51. all
  52. end
  53. end
  54. end
  55.  
  56. class TutosController < ApplicationController
  57. before_action :authenticate_user!, only: [:new, :create]
  58. before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote]
  59.  
  60.  
  61. def index
  62. #binding.pry
  63. if params[:search] || [:filter]
  64. @tutos = Tuto.search(params[:search]).includes(:user, :category)
  65. elsif
  66. @tutos = Tuto.all.includes(:user, :category)
  67. elsif
  68. @categories = Category.search(params[:filter])
  69. else
  70. @categories = Category.all
  71. end
  72. end
  73.  
  74.  
  75. def show
  76. @tuto = Tuto.find(params[:id])
  77. @user = User.all
  78. end
  79.  
  80. def new
  81. @tuto = Tuto.new
  82. end
  83.  
  84. def edit
  85. end
  86.  
  87. def create
  88.  
  89. @tuto = Tuto.new(tuto_params)
  90. @tuto.user_id = current_user.id
  91.  
  92. respond_to do |format|
  93. if @tuto.save
  94. flash[:success] = "Test"
  95. format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
  96. format.json { render :show, status: :created, location: @tuto }
  97. else
  98. format.html { render :new }
  99. format.json { render json: @tuto.errors, status: :unprocessable_entity }
  100. end
  101. end
  102. end
  103.  
  104. def update
  105. respond_to do |format|
  106. if @tuto.update(tuto_params)
  107. format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' }
  108. format.json { render :show, status: :ok, location: @tuto }
  109. else
  110. format.html { render :edit }
  111. format.json { render json: @tuto.errors, status: :unprocessable_entity }
  112. end
  113. end
  114. end
  115.  
  116.  
  117. def destroy
  118. @tuto.destroy
  119. respond_to do |format|
  120. format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' }
  121. format.json { head :no_content }
  122. end
  123. end
  124.  
  125.  
  126. def upvote
  127. @tuto.upvote_by current_user
  128. redirect_to :back
  129. end
  130.  
  131.  
  132. private
  133.  
  134.  
  135. def set_tuto
  136. @tuto = Tuto.find(params[:id])
  137. end
  138.  
  139. def tuto_params
  140. params.require(:tuto).permit(:title, :content, :id, :user_id, :category_id)
  141. end
  142. end
  143.  
  144. Started GET "/tutos?utf8=%E2%9C%93&filter=AWS-Amazon&commit=Search" for ::1 at 2016-09-28 03:30:19 +0200
  145. Processing by TutosController#index as HTML
  146. Parameters: {"utf8"=>"✓", "filter"=>"AWS-Amazon", "commit"=>"Search"}
  147.  
  148. From: Tutos/app/controllers/tutos_controller.rb @ line 7 TutosController#index:
  149.  
  150. 6: def index
  151. => 7: binding.pry
  152. 8: if params[:search] || [:filter]
  153. 9: @tutos = Tuto.search(params[:search]).includes(:user, :category)
  154. 10: elsif
  155. 11: @tutos = Tuto.all.includes(:user, :category)
  156. 12: elsif
  157. 13: @categories = Category.search(params[:filter])
  158. 14: else
  159. 15: @categories = Category.all
  160. 16: end
  161. 17: end
  162.  
  163. [1] pry(#<TutosController>)> params
  164. => {"utf8"=>"✓", "filter"=>"AWS-Amazon", "commit"=>"Search", "controller"=>"tutos", "action"=>"index"}
  165. [2] pry(#<TutosController>)>
  166.  
  167. ActiveRecord::Schema.define(version: 20160920133801) do
  168.  
  169. create_table "categories", force: :cascade do |t|
  170. t.string "name"
  171. t.text "description"
  172. t.string "image"
  173. t.datetime "created_at", null: false
  174. t.datetime "updated_at", null: false
  175. end
  176.  
  177. create_table "tutos", force: :cascade do |t|
  178. t.datetime "created_at", null: false
  179. t.datetime "updated_at", null: false
  180. t.string "title"
  181. t.text "content"
  182. t.integer "user_id"
  183. t.integer "category_id"
  184. end
  185.  
  186. add_index "tutos", ["user_id"], name: "index_tutos_on_user_id"
  187.  
  188. create_table "users", force: :cascade do |t|
  189. t.string "email", default: "", null: false
  190. t.string "encrypted_password", default: "", null: false
  191. t.string "reset_password_token"
  192. t.datetime "reset_password_sent_at"
  193. t.datetime "remember_created_at"
  194. t.integer "sign_in_count", default: 0, null: false
  195. t.datetime "current_sign_in_at"
  196. t.datetime "last_sign_in_at"
  197. t.string "current_sign_in_ip"
  198. t.string "last_sign_in_ip"
  199. t.datetime "created_at", null: false
  200. t.datetime "updated_at", null: false
  201. t.string "first_name"
  202. t.string "last_name"
  203. t.boolean "admin"
  204. end
  205.  
  206. add_index "users", ["email"], name: "index_users_on_email", unique: true
  207. add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  208.  
  209. create_table "votes", force: :cascade do |t|
  210. t.integer "votable_id"
  211. t.string "votable_type"
  212. t.integer "voter_id"
  213. t.string "voter_type"
  214. t.boolean "vote_flag"
  215. t.string "vote_scope"
  216. t.integer "vote_weight"
  217. t.datetime "created_at"
  218. t.datetime "updated_at"
  219. end
  220.  
  221. add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
  222. add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
  223.  
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement