ruby on rails - Either login form on every page or form that redirects to page -
in rails app, there lot of pages don't require signed in view. example, profile page of user. current session logic redirects previous page if page requires signed in. if doesn't though, profile page, , click sign in , sign in, takes url specified in sessionscontroller action. brings me crossroads. either need modal on every page (in layouts/application.html.erb
) has sessions form user can click sign in, model open, , when sign in refreshes page pretty user can stay on current page. however, i'm trying , getting few errors. other option have individual login page no matter page come within app, redirect to. unfortunately, i'm not sure how write code this.
here code have i'm trying have session form on every page through modal.
**sessionscontroller #create action** def create user = user.find_by(email: params[:session][:email].downcase) if user && user.authenticate(params[:session][:password]) sign_in user redirect_back_or request.original_url else flash.now[:error] = "invalid email/password combination." render 'new' end end **layouts/application.html.erb** <% unless signed_in? %> ...modal code <%= form_for(:session, url: sessions_path) %> <%= text_field ... %> <%= password_field .... %> <%= submit button %> <% end %> ... # i've shorted code here <% end %>
this form works fine on /login
page: match '/login', to: "sessions#new", via: "get"
however, when use form on other pages, error:
`missing template public/error {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. `
and page goes :
http://localhost:3000/sessions
how can make sessions form work on every page of site?? thanks
update
after playing more, realized form works when change code in sessioncontroller
redirect_back_or profile_path(user)
instead of current
redirect_back_or request.original_url
how can redirect current page i'm on instead of specifying specific page profile page???
i think found answer. please still answer if have better solution.
redirect_back_or :back
Comments
Post a Comment