2014-02-24 44 views
0

我试图遵循这个线程解决方案的方法 - 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]}. 

这似乎是逃避,它在寻找一个观点 - 但实际上我只是希望它在我的调查控制器运行的方法和更新问题正在显示。也许我正在以这种错误的方式去做,任何帮助/建议都非常感谢!

回答

3

这是因为操作已正确到达,但Rails尝试渲染某些内容。默认情况下,它将查找具有相同操作名称的视图文件。

你应该做这样的事情:

def update_question 
    set_survey 
    # do stuff 
    flash[:alert] = "getting there man" 
    redirect_to survey_path(@survey) 
end 
+0

啊,是非常有意义的 - 我可以只设置我的新PARAMS以显示新的问题。 –