为什么一种方法对一个动作完美有效,而对另一个动作不起作用?



我是rails中的新手,所以你能解释一下为什么方法survey_type适用于这个(尝试/新(

<h2 class="survey-title">
<%= @survey.name %>
<p><small><%= @attempt.survey.description %></small></p>
</h2>
<%= form_for(@attempt, url: attempt_scope(@attempt)) do |f| %>
<%= hidden_field_tag :survey_id, @survey.id %>
<ol class="questions">
<% if is_multanswer?(@survey.survey_type) %>
<%= f.fields_for :answers, get_answer_fields(@attempt) do |answer_fields| %>
<li>
<% question = answer_fields.object.question %>
<p class="question"><%= question.text %></p>
<ul class="options">
<%= collection_check_boxes('survey_attempt[answers_attributes]', question.id, question.options, :id, :text) do |b| %>
<li class="checkbox">
<%= b.label { b.check_box + b.text } %>
</li>
<% end %>
</ul>
</li>
<% end -%>
<%  else %>
<%= f.fields_for :answers, get_answer_fields(@attempt) do |answer_fields| %>
<li>
<% question = answer_fields.object.question %>
<p class="question"><%= question.text %></p>
<ul class="options">
<%= collection_radio_buttons('survey_attempt[answers_attributes]', question.id, question.options, :id, :text) do |b| %>
<li class="radio">
<%= b.label { b.radio_button + b.text } %>
</li>
<% end %>
</ul>
</li>
<% end -%>
<% end %>
</ol>
<%= f.submit "Submit", class: 'btn btn-default' %>
<% end -%>   

并且不为此工作(尝试/显示(:

<div class="container">
<h2 class="survey-title">
<%= @attempt.survey.name %>
<p><small><%= @attempt.survey.description %></small></p>
</h2>
<ol class="questions">
<% if is_multanswer?(@survey.survey_type) %>
<% @attempt.answers.each do |answer| %>
<li>
<p class="question"> <%= answer.question.text %> </p>
<ul class="options">
<% answer.question.options.each do |option| %>
<li class="checkbox">
<label>
<%= check_box_tag '', '', the_chosen_one?(answer, option), disabled: true %>
<% color = get_color_of_option(answer, option) %>
<span class="<%= color %> <%= the_chosen_one?(answer, option) %>"> <%= option.text %> <%= get_weight(option) %> </span>
</label>
<p class="answers-number"> <%= number_of_people_who_also_answered(option.id) %> </p>
</li>
<% end %>
</ul>
</li>
<% end %>
<% else %>
<% @attempt.answers.each do |answer| %>
<li>
<p class="question"> <%= answer.question.text %> </p>
<ul class="options">
<% answer.question.options.each do |option| %>
<li class="radio">
<label>
<%= radio_button_tag '', '', the_chosen_one?(answer, option), disabled: true %>
<% color = get_color_of_option(answer, option) %>
<span class="<%= color %> <%= the_chosen_one?(answer, option) %>"> <%= option.text %> <%= get_weight(option) %> </span>
</label>
<p class="answers-number"> <%= number_of_people_who_also_answered(option.id) %> </p>
</li>
<% end %>
</ul>
</li>
<% end %>
<% end %>

以下是控制器:

attempts_controller

class AttemptsController < ApplicationController
helper 'surveys'
before_filter :load_survey, only: [:new, :create]
def index
@surveys = Survey::Survey.active
end
def show
@attempt = Survey::Attempt.find_by(id: params[:id])
render :access_error if current_user.id != @attempt.participant_id
end
def new
@participant = current_user
unless @survey.nil?
@attempt = @survey.attempts.new
@attempt.answers.build
end
end
def create
@attempt = @survey.attempts.new(params_whitelist)
@attempt.participant = current_user
if @attempt.valid? && @attempt.save
correct_options_text = @survey.correct_options.present? ? 'Bellow   are the correct answers marked in green' : ''
redirect_to attempt_path(@attempt.id), notice: "Thank you for answering #{@survey.name}! #{correct_options_text}"
else
build_flash(@attempt)
@participant = current_user
render :new
end
end
def delete_user_attempts
Survey::Attempt.where(participant_id: params[:user_id], survey_id: params[:survey_id]).destroy_all
redirect_to new_attempt_path(survey_id: params[:survey_id])
end
private
def load_survey
@survey = Survey::Survey.find_by(id: params[:survey_id])
end
def params_whitelist
if params[:survey_attempt]
params[:survey_attempt][:answers_attributes] = params[:survey_attempt][:answers_attributes].map { |attrs| { question_id: attrs.first, option_id: attrs.last } }
params.require(:survey_attempt).permit(Survey::Attempt::AccessibleAttributes)
end
end
def current_user
view_context.current_user
end
end

surveys_controller

class SurveysController < ApplicationController
before_filter :load_survey, only: [:show, :edit, :update, :destroy]
def index
type = view_context.get_survey_type(params[:type])
query = if type then Survey::Survey.where(survey_type: type) else Survey::Survey end
@surveys = query.order(created_at: :desc).page(params[:page]).per(15)
end
def new
@survey = Survey::Survey.new(survey_type: view_context.get_survey_type(params[:type]))
end
def create
@survey = Survey::Survey.new(params_whitelist)
if @survey.valid? && @survey.save
default_redirect
else
build_flash(@survey)
render :new
end
end
def edit
end
def show
end
def update
if @survey.update_attributes(params_whitelist)
default_redirect
else
build_flash(@survey)
render :edit
end
end
def destroy
@survey.destroy
default_redirect
end
private
def default_redirect
redirect_to surveys_path, notice: I18n.t("surveys_controller.#{action_name}")
end
def load_survey
@survey = Survey::Survey.find(params[:id])
end
def params_whitelist
params.require(:survey_survey).permit(Survey::Survey::AccessibleAttributes << :survey_type)
end
end

和助手:

def get_color_of_option answer, option
if is_quiz?(answer.question.survey.survey_type)
if option.correct
'bg-success'
elsif the_chosen_one?(answer, option)
'bg-danger'
end
elsif is_score?(answer.question.survey.survey_type)
get_weight_html_class option
end
end
def get_survey_type survey_type
get_survey_types[survey_type] || get_survey_types.invert[survey_type]
end
def get_survey_types
{ 0 => 'quiz',
1 => 'score',
2 => 'poll',
3 => 'multanswer'
}
end
def is_quiz? something
something == 0 || something == 'quiz'
end
def is_score? something
something == 1 || something == 'score'
end
def is_poll? something
something == 2 || something == 'poll'
end
def is_multanswer? something
something == 3 || something == 'multanswer'
end

提前感谢!

在你的AttemptsController你有

before_filter :load_survey, only: [:new, :create]

这会为您的new操作设置@survey变量,但不会调用show,因此该操作永远不会获得变量集。

您可以将:show添加到:only条件中

before_filter :load_survey, only: [:new, :create, :show]

这应该解决问题

最新更新