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!

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, that makes total sense - and I can just set my new params to display the new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.