My client has asked for a single url to complete a workflow in their application: example.org/task/:token
. Where :token
is a unique id for that task.
Within the TaskController
in the index
action :token
is used to query the task object and the view is rendered based upon the Task's current state:
def index
@task = Task.where(token: params[:token])
render @task.state.to_s
end
Every state has a view and the logic for rendering the view is in the view itself (UGLY!!).
I'd like to refactor this so that Task state is used to determine which controller action to render. I can do that with render template: '#{state}/action'
. But this doesn't execute the controller action logic. Meaning I'm still stuck with controller logic in the view!
I've found this solution that works, but it's a bit ugly and breaks some of rails "magic" (have to explicitly render the view.)
My questions is, is there a better way to accomplish this within Rails while still maintaining the single url and not redirecting to a new url?