Answer question through ajax

This commit is contained in:
Amaia Castro
2016-12-28 16:21:58 +01:00
parent a6ceab6d73
commit 2693393eb0
7 changed files with 32 additions and 14 deletions

View File

@@ -50,6 +50,7 @@
//= require markdown_editor //= require markdown_editor
//= require cocoon //= require cocoon
//= require allegations //= require allegations
//= require legislation_questions
//= require custom //= require custom
var initialize_modules = function() { var initialize_modules = function() {
@@ -74,6 +75,7 @@ var initialize_modules = function() {
App.SocialShare.initialize(); App.SocialShare.initialize();
App.MarkdownEditor.initialize(); App.MarkdownEditor.initialize();
App.Allegations.initialize(); App.Allegations.initialize();
App.LegislationQuestions.initialize();
}; };
$(function(){ $(function(){

View File

@@ -0,0 +1,8 @@
App.LegislationQuestions =
initialize: ->
$('form#new_legislation_answer input.button').hide()
$('form#new_legislation_answer input[type=radio]').on
click: ->
$('form#new_legislation_answer').submit()

View File

@@ -6,10 +6,16 @@ class Legislation::AnswersController < Legislation::BaseController
load_and_authorize_resource :question, through: :process load_and_authorize_resource :question, through: :process
load_and_authorize_resource :answer, through: :question load_and_authorize_resource :answer, through: :question
respond_to :html, :js
def create def create
@answer.user = current_user @answer.user = current_user
@answer.save @answer.save
redirect_to legislation_process_question_path(@process, @question)
respond_to do |format|
format.js {}
format.html { redirect_to legislation_process_question_path(@process, @question) }
end
end end
private private

View File

@@ -0,0 +1 @@
$("#legislation-answer-form").html('<%= j render("legislation/questions/answer_form", process: @process, question: @question, answer: @answer) %>');

View File

@@ -0,0 +1,12 @@
<% if question.question_options.any? %>
<%= form_for answer, url: legislation_process_question_answers_path(process, question, answer), remote: true , html: { class: "controls-stacked participation-allowed"} do |f| %>
<% question.question_options.each do |question_option| %>
<label class="control radio <%= 'active' if @answer.legislation_question_option_id == question_option.id %>">
<%= f.radio_button :legislation_question_option_id, question_option.id, label: false, disabled: answer.persisted? %>
<span class="control-indicator"></span>
<%= question_option.value %>
</label>
<% end %>
<%= f.submit t('legislation.questions.show.answer_question'), class: "button" unless answer.persisted? %>
<% end %>
<% end %>

View File

@@ -22,19 +22,8 @@
<div class="row"> <div class="row">
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<h3 class="quiz-question"><%= @question.title %></h3> <h3 class="quiz-question"><%= @question.title %></h3>
<div class="debate-questions"> <div class="debate-questions" id="legislation-answer-form">
<% if @question.question_options.any? %> <%= render 'answer_form', process: @process, question: @question, answer: @answer %>
<%= form_for @answer, url: legislation_process_question_answers_path(@process, @question, @answer), class: "controls-stacked" do |f| %>
<% @question.question_options.each do |question_option| %>
<label class="control radio <%= 'active' if @answer.legislation_question_option_id == question_option.id %>">
<%= f.radio_button :legislation_question_option_id, question_option.id, label: false, disabled: @answer.persisted? %>
<span class="control-indicator"></span>
<%= question_option.value %>
</label>
<% end %>
<%= f.submit t('.answer_question'), class: "button" unless @answer.persisted? %>
<% end %>
<% end %>
</div> </div>
</div> </div>