Rails实例变量



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

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

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

所以我想知道是否有任何方法可以将params传递到控制器的下一个实例?或者有没有更好的方法来发送应该为该问题设置的调查?这是我可以在一个隐藏的领域"保存"的东西吗?我曾想过尝试在我的模型中保存一些东西,但要提前保存一个问题,需要删除我的验证。

这是我的问题_控制者:

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工作-如何在控制器方法之间传递值

这样做有什么不对吗?还是这是最好的路线?

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

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

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

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

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

最新更新