我正在创建一个调查应用程序,其中调查可以有许多问题和问题可以有许多调查。我想要做的是在调查显示页面上显示一个按钮“添加新问题”,允许用户在该调查中添加一个新问题。所以在我的代码中,我发送这样的调查ID:Rails实例变量
<%= link_to "Add Question", new_question_path(:survey_id => @survey.id)%>
然后,我可以设置@survey使用我发送在我的问题控制器中的参数。这在我的new方法中工作正常,但当我尝试调用create方法时抛出nil错误。我相信这是因为控制器的一个新实例正在创建,它不再有权访问我最初发送的:survey_id参数。
所以,我想知道是否有任何传递参数传递给控制器的下一个实例?或者是否有更好的方法来发送哪个调查应该为这个问题设置?这是我可以“保存”在隐藏领域的东西吗?我想过试图在我的模型中保存某些内容,但为了保存一个问题,我需要删除我的验证。
这里是我的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 %>
谢谢!很感谢任何形式的帮助!
UPDATE:
我能够使用Rails.cache.write/Rails.cache.read工作 - How to pass values between controller methods
这有什么不妥做这样或者是最好的路线?
最好的是使用嵌套路线。缓存是暂时的,所以如果缓存是齐平的,仍然有可能为零。取决于你如何缓存它,其他用户可能会得到错误的调查ID。 – sonnyhe2002
我最初使用嵌套路线进行调查>问题,但是当我嵌套问题>答案时,开始遇到很多问题。你不会碰巧知道一个好的教程或资源如何处理双重嵌套的路线/形式,你会吗? –
对于你不需要双重嵌套路线的答案,所以你可以定义两个问题资源,一个嵌套在调查中,另一个不嵌套。不是那个将仍然持有资源的答案 – sonnyhe2002