ruby on rails - How to render a view from a view using a condition? -
i'm testing out ror , trying figure out functionality allows users valid session skip logging in.
the login button prompts login page, whereas want make people session skip , redirected show view using conditional statement. (checks via current_user) (if need other code comment)
here's login view
<%= form_for :session, url: '/login' |s| %> <%= s.text_field :username, :placeholder => 'username' %> <%= s.password_field :password, :placeholder => 'password' %> <%= s.submit "login" %> <% if current_user %> <!-- write here? --> <!-- maybe should put in controller? --> <% end %> <% if flash[:notice] && current_user %> <div class="notice"><%= flash[:notice] %></div> <% end %>
here's application controller containing current_user function. (just in case)
class applicationcontroller < actioncontroller::base protect_from_forgery with: :exception helper_method :current_user def current_user @current_user ||= user.find_by(id: session[:id]) if session[:id] end end
not sure sessionscontroller looks like, add this:
class sessionscontroller < actioncontroller::base before_action :check_current_user def check_current_user redirect_to :root, success: "you logged in." if current_user end end
then can remove if current_user
statement in login view. , login links write:
<%= link_to 'login', new_session_path unless current_user %>
this way login button shows if current_user
nil.
also, take @ rails tutorial chapter 8: basic login more information
Comments
Post a Comment