0

I was trying to follow the solution on this thread - Rails 3 link or button that executes action in controller

I defined :update_question in my routes.rb file:

  resources :surveys do
    put :update_question, :on => :member
  end

and in my controller:

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

And listed the link here in the html:

<%= link_to "Next Question", update_question_survey_path(@survey), {:method => :put} %>

However when I click the link I get this error:

Template is missing
Missing template surveys/update_question, application/update_question with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.

Which seems to elude that it's looking for a view - but really I just want it to run the method in my survey controller and update the question that's being displayed. Perhaps I'm going about this the wrong way, any help/suggestions is much appreciated!

Community
  • 1
  • 1
Tom Hammond
  • 5,842
  • 12
  • 52
  • 95

1 Answers1

3

That's because the action is correctly reached, but then Rails tries to render something. By default, it will look for a view file with the same name of the action.

You should do something like this:

def update_question
  set_survey
  # do stuff
  flash[:alert] = "getting there man"
  redirect_to survey_path(@survey)
end
tompave
  • 11,952
  • 7
  • 37
  • 63