セッション値に基づいて特定のビューをレンダリングするアプリケーションを作成しようとしています。ただし、私はセッション値を格納することができません。セッション値を取得できません
私のコントローラのようなある:
<h1>Question</h1>
<div align="center">
<% if (session[:state]).to_s == 'question_one' %>
<%= render partial: 'layouts/question_one' %>
<% else %>
<%= render partial: 'layouts/question_two' %>
<% end %>
</div>
<div class="buttons">
<%= link_to "Next", question_path, class: "btn btn-large btn-primary" %>
</div>
そしてroutes.rb
に、question_pathは、例えば次のように定義されます:
class MyController < ApplicationController
def question
if session[:state].nil?
session[:state] = 'question_one'
end
end
def next_page
# state pattern would be much better, this is a bit ugly
if session[:state].nil? || session[:state] == 'question_one'
session[:state] = 'question_two'
elsif session[:state] == 'question_two'
session[:state] = 'question_one'
end
end
end
マイビューは、(名前付きquestion.html.erb
)である
resources :question do
collection do
get :next_page
end
end
ただし、session[:state]
は常にです、なぜ?
注:この質問は、Ruby on Railsがどのように機能するかに関するものであり、私はサイト自体の詳細の大部分を省略し、質問に関連するものだけを残しました。
どのように 'resources:question'を' QuestionController'ではなく 'MyController'にルーティングしていますか?あなたは間違いなくここで正しいコントローラのアクションを打つのですか? – gwcodes