Heyya guys.
So i thought about this coolio idea, if you are logged in then you get some sort of dashboard, else you get an information/login/sign up page.. So how do i do that..
I mostly wants to do this in Routes = not something like
def index
if current_user.present?
render :action => 'logged_in'
else
render :action => 'logged_out'
end
end
thanks in advance!
/ Oluf Nielsen
Think you may have been looking for this:
authenticated :user do
root :to => "dashboard#show"
end
root :to => "devise/sessions#new"
Note: it's authenticate*d*
I too wanted this in my app, here's what I came up with.
MyCoolioApp::Application.routes.draw do
root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? }
root :to => 'welcome#index'
get "/" => 'users#dashboard', :as => "user_root"
# ..
end
In Rails 3 you can use Request Based Contraints to dynamically map your root route. The solution above works for the Devise authentication gem but can be modified to support your own implementation.
With the above root_path or / will route to a WelcomeController#index action for un-authenticated requests. When a user is logged in the same root_path will route to UsersController#dashboard.
Hope this helps.
I have the same problem and I solved it with this:
authenticated :user do
root :to => "wathever#index"
end
unauthenticated :user do
devise_scope :user do
get "/" => "devise/sessions#new"
end
end
Hope it helps.
are you using devise's before filters?
class FooController < ActionController::Base
before_filter :authenticate_user!
...
Why don't you try altering the default login views so they have the info/login/signup infos you want.
Here's what I'm using in my application layout file right now. Haven't broken it out into partials yet:
<% if user_signed_in? %>
<%= current_user.email %> |
<%= link_to "Logout", destroy_user_session_path %>
<% else %>
<%= link_to "Login", new_user_session_path %> |
<%= link_to "Register", new_user_registration_path %>
<% end %>
Related
Can I disable auto redirect in ruby on rails, here my example: In my controllers
class WelcomeController < ApplicationController
def index
#a = params[:link]
if #a != nil
Kernel.system("ls")
redirect_to root_path
else
end
end
end
I don't want to redirect to root_path and stay in current pages, so I remove redirect_to root_path, like this:
def index
#a = "abcd"
if #a != nil
Kernel.system("ls")
else
end
end
But my app still auto redirect to root_path, I don't know how to fix that, please! help me:)
my routes:
root 'welcome#index'
match '/upload', to: 'welcome#index', via: 'post'
my html:
<%= form_tag(upload_path, :id => "form_check") do %>
<%= text_field_tag :link %>
<%= submit_tag("Process")%>
<%end%>
I think your problem isn't really auto redirect. You are "redirected" to your index because this line doesn't match: match '/upload', to: 'welcome#index', via: 'post'.
What you should do is to separate index and upload. It will be far easier to manage.
routes:
root 'welcome#index'
get 'form' => 'welcome#form', as: :form_path
post 'upload' => 'welcome#upload', as: :upload_path
form page:
<%= form_tag(upload_path, :id => "form_check") do %>
<%= text_field_tag :link %>
<%= submit_tag("Process")%>
<%end%>
And then you can create your new actions inside welcome, called form and upload. The last one redirect you to /form, so you can see your form again.
I have the following in my routes.rb:
...
authenticated :user do
root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
root to: 'editions#index'
end
...
resources :editions, :path => "games" do
collection do
get 'to_review'
post 'review'
get 'existing_work'
end
member do
put 'split'
end
resources :expansions do
end
end
...
will_paginate is like this:
<%= will_paginate #collection, renderer: BootstrapPagination::Rails %>
And the result links are like this:
http://localhost:3000/?page=2
If I make unauthenticated root be other thing than editions#index, the links are correct, like this:
http://localhost:3000/games?page=2
I've tried using this:
<%= will_paginate #collection, :params => {:controller => '/editions', :action => 'index'}, renderer: BootstrapPagination::Rails %>
But it does not force will_paginate to use the right route. And if I try this:
<%= will_paginate #collection, :params => {:controller => editions_path }, renderer: BootstrapPagination::Rails %>
Rails give me route errors:
No route matches {:action=>"index", :controller=>"games", :page=>2}
I've inverted my routes.rb and it works now. It seems order is important and I was not aware.
...
resources :editions, :path => "games" do
collection do
get 'to_review'
post 'review'
get 'existing_work'
end
member do
put 'split'
end
resources :expansions do
end
end
authenticated :user do
root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
root to: 'editions#index'
end
I'm setting my new Rails website in more than one language, and I'm having problems with the routes. I'm following the instruction of the book 'Agile web development with Rails 4'.
The browser print me this error but I can see that routes are created correctly, so:
What am I doing wrong? (At the end of this message I'll attach all my routes)
No route matches [POST] "/en/home"
When I try putting the routes directly in the browser ("localhost:3000/en" OR "localhost:3000/es") everything works OK. The error prints only when I change my language's switcher. That's why I think the routes are correctly set, and I think is a problem of my switcher or the controller...?
This is the code in the application.html.rb (basically a switcher between languages):
<%= form_tag home_path, class: 'locale' do %>
<%= select_tag 'set_locale',
options_for_select(LANGUAGES, I18n.locale.to_s),
onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
This is the configuration of my routes.rb file:
Group::Application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
scope '(:locale)' do
resources :posts
resources :contacts
root "posts#home"
get "/home" => "posts#home"
get "/contact" => "contacts#new"
# static pages
get "/investment" => "contents#investment"
get "/partner-with-us" => "contents#partner", as: "partner"
get "/our-companies" => "contents#companies", as: "companies"
get "/site-map" => "contents#sitemap", as: "sitemap"
get "/terms-and-conditions" => "contents#terms", as: "terms"
get "/privacy" => "contents#privacy"
end
end
This is a file created in /config/initializers/i18n.rb:
#encoding: utf-8
I18n.default_locale = :en
LANGUAGES = [
['English', 'en'],
["EspaƱol".html_safe, 'es']
]
And finally, this is the code for my posts_controller.rb, because here is where I create an action "home" in order to put the last post in the home page:
class PostsController < ApplicationController
def index
#posts = Post.all.order("created_at desc")
end
def show
#post = Post.find(params[:id])
end
def home
if params[:set_locale]
redirect_to home_url(locale: params[:set_locale])
else
#posts = Post.all.order("created_at desc")
end
end
end
try to add :method => :get to your form, like this
<%= form_tag home_path, class: 'locale', :method => :get do %>
I inherited an old Rails app and I'm really struggling with this error:
ActionController::RoutingError
(No route matches {:controller=>"users", :action=>"profile"}):
app/views/layouts/application.html.erb:59:in
`_app_views_layouts_application_html_erb__105<snip>'
app/controllers/users_controller.rb:40:in `index'
I ONLY get this error when I log in as admin, not as any other user.
Here is my routes.rb
Vvault::Application.routes.draw do
resources :orders
devise_for :users
resources :videos
resources :users
root :to => "users#index"
match 'users/:id/profile' => 'users#profile', :as => :user_profile
end
I think this is the relevant snippet from users_controller.rb:
def index
if current_user.admin?
# admin sees all users
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
else
redirect_to "/users/" + current_user.id.to_s() and return
end
end
I think this is the relevant snippet from application_html.erb:
<div class="sidebar">
<% if user_signed_in? %>
<%= link_to "My account", user_profile_path, :method => :get %>
<% end %>
</div>
If i comment out the third line of application_html.erb I can login as admin, but obviously the profile link does not render for any user.
Any help appreciated.
Thanks!
Try:
<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>
Your user_profile_path helper needs an id to be passed in, as the corresponding route has an :id parameter.
The error message is telling you that no route exists for that controller/action combination without any other parameters.
Or, you need to define a route without an id parameter where the controller automatically loads the current user's profile
You need to provide the id to user_profile_path. But since that route points to the user's account, there is no point in setting the id, because it should always be the current user (is that your intention?). If this is the case, then you can rewrite the route as:
match 'users/profile' => 'users#profile', :as => :user_profile
If not, then provide an id to the helper method for your route.
I have been working with rails3, here the view.html.erb form have one login button, so when i click on that button, gives no routes matches :controller => 'home', :action => 'login'. But i have put that in routes.rb. Why this happening?
view.html.erb
<%= form_tag( { :controller => 'home', :action => 'login' }, { :method
=> 'post'}) do %>
<%= text_field(:name, :name, :class => "span2",:placeholder =>
"Username") %>
<%= password_field_tag(:password, :password, :class =>"span2") %>
<%= submit_tag "Login", :class => "btn btn-primary" %>
<% end %>
**routes.rb**
resources :home
resources :home do
post :login, :on => :member
end
**homecontroller.rb**
class HomeController < ApplicationController
def login
end
end
You have defined "resources :home" twice, first declaration is useless and overrides second.
Since you used resources to define your routes (which is recomended) you should use the helper method generated, in this case its login_home_path instead of the old syntax { :controller => 'home', :action => 'login' }
First,
You declare resources :home two times.
try this way in your routes.rb
resources :home
match '/login', to: 'home#login'
and use login_path in submit tag.
I would prefere for login, logout, create Sessions controller
rails generate controller Sessions --no-test-framework
and for login create new method and for logout(signout) create destroy method