我试图按照此主题的解决方案 - Rails 3 link or button that executes action in controller
我在routes.rb文件中定义了:update_question:
resources :surveys do
put :update_question, :on => :member
end
并在我的控制器中:
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]
before_action :set_question
# GET /surveys
# GET /surveys.json
def index
@surveys = Survey.all
end
# GET /surveys/1
# GET /surveys/1.json
def show
end
def survey
@survey = Survey.find(params[:survey_id])
end
# GET /surveys/new
def new
@survey = Survey.new
end
# GET /surveys/1/edit
def edit
end
def update_question
flash[:alert] = "getting there man"
end
并在html中列出了链接:
<%= link_to "Next Question", update_question_survey_path(@survey), {:method => :put} %>
但是当我点击链接时出现此错误:
Template is missing
Missing template surveys/update_question, application/update_question with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.
似乎可以避免它正在寻找一个视图 - 但实际上我只是希望它在我的测量控制器中运行该方法并更新正在显示的问题。也许我会以错误的方式解决这个问题,非常感谢任何帮助/建议!
答案 0 :(得分:3)
那是因为操作已正确到达,但Rails尝试渲染某些内容。默认情况下,它将查找具有相同操作名称的视图文件。
你应该这样做:
def update_question
set_survey
# do stuff
flash[:alert] = "getting there man"
redirect_to survey_path(@survey)
end