【问题标题】:Rails Instance VariablesRails 实例变量
【发布时间】:2014-02-23 20:33:52
【问题描述】:

我正在创建一个调查应用程序,其中调查可以有很多问题,问题可以有很多调查。我想做的是在调查显示页面上显示一个“添加新问题”按钮,允许用户向该调查添加新问题。所以在我的代码中,我像这样发送调查 id:

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

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

所以我想知道是否有将参数传递给控制器​​的下一个实例?还是有更好的方法来发送应该为该问题设置的调查?这是我可以“保存”在隐藏字段中的东西吗?我曾想过尝试在我的模型中保存一些东西,但要早点保存一个问题需要我删除我拥有的验证。

这是我的问题控制器:

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

这样做有什么问题吗?还是最好的方法?

【问题讨论】:

  • 最好是使用嵌套路由。缓存是临时的,所以如果缓存是刷新的,仍然有可能为零。根据您缓存它的方式,其他用户可能会得到错误的调查 ID。
  • 我最初使用嵌套路由进行调查 > 问题,但是当我嵌套问题 > 答案时开始遇到很多问题。您不会碰巧知道有关如何处理双嵌套路由/表单的好教程或资源吗?
  • 对于您不需要双重嵌套路由的答案,您可以定义两个问题资源,一个嵌套在调查下,一个不嵌套。没有的人仍将拥有答案资源
  • 这太酷了,我不知道你能做到!

标签: ruby-on-rails


【解决方案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])

如果问题始终属于调查,最好嵌套路线,以便您始终可以检查您正在处理的调查。

【讨论】:

  • 您能否举例说明将其添加到隐藏字段中的效果?我一直找不到一个很好的例子。我最初是在嵌套这个问题。但后来我遇到了问题中嵌套答案的问题,并选择将它们分开
  • 另外,如果您让用户创建调查和问题,您可能需要检查survey_id 不属于其他用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多