Rails实例变量

时间:2014-02-23 20:33:52

标签: ruby-on-rails

我正在努力创建一个调查应用程序,其中调查可以包含许多问题,并且可以进行许多调查。我想做的是在调查显示页面上显示一个“添加新问题”按钮,允许用户向该调查添加新问题。所以在我的代码中,我发送调查ID如下:

<%= link_to "Add Question", new_question_path(:survey_id => @survey.id)%>

然后我可以使用我在问题控制器中发送的参数来设置@survey。这在我的:new方法中运行正常,但是当我尝试调用:create方法时抛出一个nil错误。我相信这是因为创建了一个新的控制器实例,它不再能访问我最初发送的:survey_id参数。

所以我想知道是否还有将params传递给控制器​​的下一个实例?或者是否有更好的方法来发送针对该问题的调查?这是我可以在隐藏的领域“保存”的东西吗?我想过尝试在我的模型中保存一些东西,但是为了先保存一个问题,我需要删除我的验证。

这是我的question_controller:

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]
  before_action :set_survey, only: [:new, :create]
  # GET /questions
  # GET /questions.json
  def index
    @questions = Question.all
  end

  # GET /questions/1
  # GET /questions/1.json
  def show
    @answers = @question.answers
  end

  # GET /questions/new
  def new
    @question = Question.new
  end

  # GET /questions/1/edit
  def edit
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = Question.new(question_params)

    respond_to do |format|
      if @question.save
        @survey.questions << @question 
        format.html { redirect_to @question, notice: 'Question was successfully created.' }
        format.json { render action: 'show', status: :created, location: @question }
      else
        format.html { render action: 'new' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /questions/1
  # PATCH/PUT /questions/1.json
  def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end

    def set_survey
      @survey = Survey.find(params[:survey_id])
      flash[:alert] = "Survey is " + @survey.to_s
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:title, :single_response, :surveys, :surveytizations)
    end
end

我正在用以下形式创建问题:

<%= form_for(@question) do |f| %>
  <% if @question.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>

      <ul>
      <% @question.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :single_response %><br>
    <%= f.check_box :single_response %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

谢谢!非常感谢任何帮助!

更新:

我能够使用Rails.cache.write / Rails.cache.read工作 - How to pass values between controller methods

这样做是不是有什么问题,还是最好的路线?

1 个答案:

答案 0 :(得分:1)

我认为您需要将survey_id存储在隐藏字段中。然后您可以从问题控制器访问它。在您看来:

<%= form_for(@question) do |f| %>
  <%= f.hidden_field :survey_id %>
#rest of form

您可能还需要将new操作更改为以下内容:

@question = Question.new(:survey_id => params[:survey_id])

如果问题总是属于调查问题,那么嵌套路线可能是个好主意,这样您就可以检查自己正在进行的调查。