Guest User

Untitled

a guest
Jul 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. class Question < ApplicationRecord
  2.  
  3. belongs_to :question_status
  4. belongs_to :user
  5. has_many :choices
  6.  
  7. accepts_nested_attributes_for :choices, reject_if: proc { |attributes| attributes[:content].blank? }, limit: 5
  8.  
  9. validates :content, :source, presence: true
  10. validate :check_number_of_choices,
  11.  
  12.  
  13. def check_number_of_choices
  14. if self.choices.select{|c| c.content.present?}.size != 5
  15. self.errors.add :choices, I18n.t("errors.messages.number_of_choices")
  16. end
  17. end
  18.  
  19. end
  20.  
  21. <%= form_with(model: question, local: true) do |form| %>
  22. <% if question.errors.any? %>
  23. <div id="error_explanation">
  24. <h2><%= pluralize(question.errors.count, "error") %> prohibited this question from being saved:</h2>
  25.  
  26. <ul>
  27. <% question.errors.full_messages.each do |message| %>
  28. <li><%= message %></li>
  29. <% end %>
  30. </ul>
  31. </div>
  32. <% end %>
  33.  
  34. <div class="field">
  35. <%= form.label :content %>
  36. <%= form.text_area :content %>
  37. </div>
  38.  
  39. <div class="field">
  40. <%= form.label :source %>
  41. <%= form.text_field :source %>
  42. </div>
  43.  
  44. <div class="field">
  45. <%= form.label :year %>
  46. <%= form.number_field :year %>
  47. </div>
  48.  
  49. <div class="field">
  50. <%= form.label :choices %>
  51.  
  52. <%= form.fields_for :choices do |choice| %>
  53. <p>
  54. - <%= choice.text_field :content %>
  55. </p>
  56. <% end %>
  57.  
  58. </div>
  59.  
  60.  
  61. <div class="actions">
  62. <%= form.submit %>
  63. </div>
  64. <% end %>
  65.  
  66. class QuestionsController < ApplicationController
  67. before_action :set_question, only: [:show, :edit, :update, :destroy]
  68.  
  69. # GET /questions
  70. # GET /questions.json
  71. def index
  72. @questions = Question.all
  73. end
  74.  
  75. # GET /questions/1
  76. # GET /questions/1.json
  77. def show
  78. end
  79.  
  80. # GET /questions/new
  81. def new
  82. @question = Question.new
  83. 5.times { @question.choices.build }
  84. end
  85.  
  86. # GET /questions/1/edit
  87. def edit
  88. end
  89.  
  90. # POST /questions
  91. # POST /questions.json
  92. def create
  93. @question = Question.new(question_params)
  94. @question.question_status = QuestionStatus.find_or_create_by(name: "Pending")
  95. @question.user = current_user
  96.  
  97. respond_to do |format|
  98. if @question.save
  99. format.html { redirect_to @question, notice: 'Question was successfully created.' }
  100. format.json { render :show, status: :created, location: @question }
  101. else
  102. format.html { render :new }
  103. format.json { render json: @question.errors, status: :unprocessable_entity }
  104. end
  105. end
  106. end
  107.  
  108. # PATCH/PUT /questions/1
  109. # PATCH/PUT /questions/1.json
  110. def update
  111. respond_to do |format|
  112. if @question.update(question_params)
  113. format.html { redirect_to @question, notice: 'Question was successfully updated.' }
  114. format.json { render :show, status: :ok, location: @question }
  115. else
  116. format.html { render :edit }
  117. format.json { render json: @question.errors, status: :unprocessable_entity }
  118. end
  119. end
  120. end
  121.  
  122. # DELETE /questions/1
  123. # DELETE /questions/1.json
  124. def destroy
  125. @question.destroy
  126. respond_to do |format|
  127. format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
  128. format.json { head :no_content }
  129. end
  130. end
  131.  
  132. private
  133. # Use callbacks to share common setup or constraints between actions.
  134. def set_question
  135. @question = Question.find(params[:id])
  136. end
  137.  
  138. # Never trust parameters from the scary internet, only allow the white list through.
  139. def question_params
  140. params.require(:question).permit(:content, :source, :year, :question_status_id, :user_id, choices_attributes: [:id, :content])
  141. end
  142. end
Add Comment
Please, Sign In to add comment