I am trying to get my breadcrumbs to follow my navigation history through different controllers
Application Controller
add_breadcrumb 'Home', root_path
In my public_pages controller
class PublicPagesController < ApplicationController
def index
end
def news
add_breadcrumb "News", news_path
add_breadcrumb "Contact us", contact_path
end
def contact_us
add_breadcrumb "News", news_path
add_breadcrumb "Contact us", contact_path
end
so i have another controller called private_pages which is only accessible when a user logs in, and this has its own root_path,
How would i show breadcrumbs when accessing different actions in different controllers
Thanks
First, add the home breadcrumb to your ApplicationController as that should be registered for every request. If your application is not publicly accessible in this regard, then disregard that, and keep the home breadcrumb in your PublicPagesController before the methods.
Then, update your PublicPagesController:
class PublicPagesController < ApplicationController
def index
end
def news
# to show Home / Contact Us / News
add_breadcrumb "Contact Us", news_path
add_breadcrumb "News", news_path
end
def contact_us
add_breadcrumb "Contact Us", news_path
end
end
The above assumes that add_breadcrumb "Home", news_path is called in your ApplicationController.
In regards to bootstrap conflicts or integration, see these two:
https://github.com/weppos/breadcrumbs_on_rails/issues/24
https://gist.github.com/2400302
If you want to amend the home breadcrumb based on whether user is logged in or not, add a before_filter to your ApplicationController:
before_filter :set_home_breadcrumb
def set_home_breadcrumb
if user_signed_in?
add_breadcrumb "Home", :user_home_path
else
add_breadcrumb "Home", :root_path
end
end
Related
I have a created a rails 5 app using devise gem that has both a user and admin sign in. They are separate modals and have found in my lessons and created the logic for the admin to be directed to the dashboard after signing in however what is the best method to direct the user to the home page after signing in. Below is my set up for the admin and it works for the admin however when I sign in as the user they are directed to the dashboard as well. Outcome I need to see is the user goes to home page after signing in and admin goes to dashboard.
routes:
root 'pages#home'
devise_for :users,
path: '',
path_names: {sign_in: 'user_login', sign_out:
'user_logout', edit: 'user_profile', sign_up: 'user_registration'},
controllers: { omniauth_callbacks:
'users/omniauth_callbacks', registrations: 'registrations' }
devise_for :admins,
path:'',
path_names: {sign_in: 'login', sign_out:
'logout', edit: 'profile', sign_up: 'registration'},
controllers: { omniauth_callbacks:
'admins/omniauth_callbacks', registrations: 'registrations' }
resources :admins, only: [:show]
resources :photos
resources :locations
resources :deals
get 'dashboard' => 'admin_home#dashboard'
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if:
:devise_controller?
protected
def after_sign_in_path_for(_resource_or_scope)
dashboard_path
end
You can use devise methods for this purpose where you can define paths based on some custom checks i.e., user role.
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end
end
Reference: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in
I am using gem devise. And I overrided devise registrations controller and everything went great, but the problem is redirect path after it is saved. What I want to do is after user saved, it redirects to profile_path, but what I have now is user need to sign in before it redirect to profile path. How can I solve that?
Here is my register controller:
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
#user= User.new(params[:user])
if #user.save
redirect_to profile_path, notice: 'User was successfully created.'
else
render action: "new"
end
end
def update
super
end
end
And this is my application controller which control the path after sign up and sign in:
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(resource)
if request.path !~ /^\/admins\//i
resource.sign_in_count <= 1 ? '/profile' : root_path
end
end
end
Before I override the register controller, redirect after sign up went great. Would be really glad if anyone could help. Thanks.
You have to sign the user in in your create method:
if #user.save
sign_in(resource_name, resource)
current_user = #user # !! now logged in
redirect_to profile_path, notice: 'User was successfully created.'
else
You can look at the original create method in Devise::RegistrationsController to see how this works.
I have a simple static website written in rails 3.
The site has one controller called pages and each static page is served as view. Such as pages/home, pages/about, pages/prices, etc. This all works great.
I've now run into a problem where I need to add a simple contactus feature but I'm struggling to get my head round the model/controller/views for this.
I already have a pages controller with a contactus view, that view has details addresses etc. Now I somehow need to get a message model into the contactus view so I can populate the model attirbutes and send the email.
Can I / Should I just create a new message model from within the Pages Controller as in ,
class PagesController < ApplicationController
def contact
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
# TO DO send message here using OS mail program.
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
end
def about
end
def products
end
def portfolio
end
def services
end
end
Or should I take out the contactus view from the pages controller and make new controller called messages ?
Thanks.
I would have a separate controller called contact for example with new and create actions
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.new_message(#message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
Then a separate model to handle your messages
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body, :file
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
end
your attributes can be anything you like, obviously this is just an example of what you can do
I have a situation where a company is managed by a user. i.e.: A user can create, read, update and delete their own companies. But I'd also like that same user to access a list of all companies in the system, even when logged out.
e.g.:
user_a manages the following companies: company_a and company_b
user_b manages the following companies: company_c and company_d
user_a should be able to see a list of his own companies (a and b) as well as a list of all companies (a, b, c, and d)
What's the best way to handle this in the controllers?
Idealy, I'd like to have it setup under 2 separate routes as follows:
/companies
/users/1/companies
Should I have one controller for companies, or multiple? and how would that work?
I'm looking for best practices in this type of scenario.
In your situation approach can be:
Use Devise RubyGem to handle authentication. https://github.com/plataformatec/devise
Create or Scaffold simple CompaniesController with RESTful actions set: index, new, create, edit, udpate, destroy actions.
Add before_filter in CompaniesController to restrict access to action which require user authentication:
before_filter :authenticate_user!, :except => [:public_list]
You should have has_many assosiation between User and Company ActiveRecord models, to access companies collection of current_user.
Here goes example code:
Routing:
resources :users do
resources :companies
end
match '/companies' => 'companies#public_list', :as => :public_companies_list
Controller:
class CompaniesController < ApplicationController
before_filter :authenticate_user!, :except => [:public_list]
def index
#companies = current_user.companies
end
def show
#company = current_user.companies.find(params[:id])
end
def new
#company = current_user.companies.new
end
def edit
#company = current_user.companies.find(params[:id])
end
def create
#company = current_user.companies.new(params[:company])
respond_to do |format|
if #company.save
format.html { redirect_to #company, notice: 'Company was successfully created.' }
else
format.html { render action: "new" }
end
end
end
def update
#company = current_user.companies.find(params[:id])
respond_to do |format|
if #company.update_attributes(params[:company])
format.html { redirect_to #company, notice: 'Company was successfully updated.' }
else
format.html { render action: "edit" }
end
end
end
def destroy
#company = current_user.companies.find(params[:id])
#company.destroy
respond_to do |format|
format.html { redirect_to companies_url }
end
end
end
For public companies list add this method:
def public_list
#companies = Company.all
end
IMHO if all user can see all companies it's perfect to have one controller to get this job. Just in template you can check if current user is author of specified company and then add link to edit this company etc. if you want of course.
I'm trying to set up some semistatic page in a rails 3 app
I've created a Pages controller with some non restful actions
class PagesController < ApplicationController
def home
end
def about
end
def contact
end
def monday
end
def saturday
end
def sunday
end
end
It's showing at /pages/home etc.
Is there a way to re-route the pages so that they show under /home etc.
I've tried
resources :pages, :path => '/' do
#blah
end
but I get an error message telling me that the :action => show is missing.
Is it possible to apply a setting to all non restful actions?
You could add collection routes:
resources :pages do
collection do
get 'home'
get 'about'
get 'contact'
...
end
end