Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. class FaqsController < ApplicationController
  2. before_filter :authenticate_user!, :except => [:frequently_asked_question]
  3. before_filter :correct_user, :only => [:index, :show, :destroy,:create, :edit,:update,:new]
  4. before_action :set_faq, only: [:show, :edit, :update, :destroy]
  5. respond_to :html, :xml, :json
  6.  
  7. def index
  8. @faqs = Faq.all
  9. respond_with(@faqs)
  10. end
  11.  
  12. def show
  13. respond_with(@faq)
  14. end
  15.  
  16. def new
  17. @faq = Faq.new
  18. 1.times do
  19. @faq.faq_questions.build
  20. end
  21. respond_with(@faq)
  22. end
  23.  
  24. def edit
  25. end
  26.  
  27. def create
  28. @faq = Faq.new(faq_params)
  29. @faq.save
  30. redirect_to faqs_path
  31. end
  32.  
  33. def update
  34. @faq.update(faq_params)
  35. redirect_to faqs_path
  36. end
  37.  
  38. def destroy
  39. @faq.destroy
  40. respond_with(@faq)
  41. end
  42.  
  43. def frequently_asked_question
  44. if params[:search].present?
  45. search_condition = "%" + params[:search] + "%"
  46. @faqss = Faq.where('title LIKE ? ',search_condition)
  47. if @faqss.present?
  48. @faqs = @faqss
  49. else
  50. faq_question = FaqQuestion.where('question LIKE ? OR answer LIKE ?',search_condition, search_condition )
  51. @a = []
  52. faq_question.each do |fa|
  53. @a << fa.faq_id
  54. end
  55. @faqs = []
  56. @a.uniq.each do |fa|
  57. @faqs << Faq.find(fa)
  58. end
  59. end
  60. @faqs
  61. else
  62. @faqs = Faq.all
  63. end
  64. end
  65.  
  66. private
  67. def set_faq
  68. @faq = Faq.find(params[:id])
  69. end
  70.  
  71. def faq_params
  72. params.require(:faq).permit(:question, :answer,:title, faq_questions_attributes: [:id,:question,:answer,:faq_id,:_destroy])
  73. end
  74.  
  75. def correct_user
  76. @user = User.find_by_id_and_admin(current_user.id, true)
  77. redirect_to(root_path, :notice => "Sorry, you are not allowed to access that page.") unless current_user=(@user)
  78. end
  79.  
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement