I got link on show like below
I Want http://localhost:3000/admin/flipcart
(flipcart is company name so i want each company separate link.)
If I do http://localhost:3000/admin/flipcart so it show flipcart's show page with out login and authentication.
So how can i do.
You could try something like this inside your routes.rb:
get '/admin/:company', as: :admin_company_index, to: 'admin#index'
Then inside the controller just load the company passed through params before you perform any actions:
class AdminController < ApplicationController
before_action :load_company
private
def load_company
#company = Company.where(["name = :c", {c: params[:company]} ])
end
end
Related
Let's say I have a Rails Website or Application
I have controllers for pages and posts
I want to create on my homepage section where I can see all pages and below that section for the posts
What is the common way to do that with the routes.rb? since I have two controller I don't know how to create the homepage
To make your code more clear you can use separated controller for that. For example create HomepageController with action home and prepare all resources you need there:
class HomepageController < ApplicationController
def home
#pages = Page.where(....)
#posts = Post.where(....)
end
end
Create corresponding view file - views/homepage/home.html.erb
Then at the end of your routes.rb add:
root to: "homepage#home"
I am having some difficulty figuring out how to route static pages in rails 4. I have created a controller called PagesController and so I also have a views folder called pages with the oakville.html.erb file in it.
My controller looks like this:
class PagesController < ApplicationController
def our_mission
end
end
My routes file looks like this:
get "oakville", :to => "pages#oakville"
I am assuming that I should be able to get to this page by going to localhost:3000/oakville ??
Yes, but you need to add a controller action for oakville
class PagesController < ApplicationController
def oakville
end
end
Also, you will need to create oakville.html.erb and put this into your views/pages directory
The methods in your controller are called actions, and for each static page that you want to be able to navigate to you will need a corresponding controller action. When a person (or link) navigates to yoursite/oakville your routes file needs to know which controller action to perform for the oakville branch of the url.
In the routes that you have shown, get "oakville", :to => "pages#oakville" you are asking the controller to render the oakville action. But there is no oakville action in your controller. Add one and problem solved:
class PagesController < ApplicationController
def our_mission
end
def oakville
end
end
The route you've shown and the action you've shown are completely unrelated.
If you want to route a url like http://www.example.com/oakville to an action called our_mission on the Pages controller, the route looks like this:
get 'oakville' => 'pages#our_mission'
What you've written indicates you expect an action called oakville to exist, and according to the code you've provided, it doesn't.
SingularI have this problem. I defined in route file my route:
namespace :admin do
root to: "home#index"
resources :define_user
end
I created users controller:
class DefineUsersController < ApplicationController
def create
...
end
def destroy
...
end
end
I created in views new folder 'define_users' with file 'show.html.haml'. I call it using link_to:
=link_to 'User', admin_define_user_path
And I get above error. I would like to stay with singular name. Thank for all answers.
You are trying to access show route without id of DefineUser object
= link_to 'User', admin_define_user_path(define_user)
Where define_user is an object of DefineUser class or id of this object
First of all, if you want to link_to some define_user, you have to provide object or id. Something like this: admin_define_user_path(#define_user).
To display all possible routes type rake routes in console.
Also resources should be in plural form.
Moreover you have to provide namespace in controller.
P.S.
As I see, it is a very bad idea to work with model called DefineUser. It's not a rails way. Just use User. DefineUser is a good name for method, but not model.
So, the best idea to handle your code:
routes.rb
namespace :admin do
root to: "home#index"
resources :users
end
users_controller.rb
class Admin::UsersController < ApplicationController
def index
...
end
end
view
= link_to 'Users', admin_users_path # for index
= link_to 'User', admin_user_path(#user) # for one user
Good idea to separate controllers by namespace. For example, you can have next structure:
application_controller.rb
admin #folder
L base_controller.rb
L users_controller.rb
L ..._controller.rb
So your base_controller should be inherited from application_controller
class Admin::BaseController < ApplicationController
layout 'admin_layout' # Different layout for all admin pages
Other controller in admin namespace will be inherited from base_controller
class Admin::UsersController < Admin::BaseController
def create
...
end
How do you setup your views, controllers and routes?
One controller for everything the control panel does, or many?
Firstly, let's try to think how we would view the various panels. Let's say our control panel is pretty simple. We have one panel to show all the users who have signed-up and can CRUD them, and another panel to show all of the images that have uploaded, and we can carry up CRUD on those too.
Routes:
scope path: 'control_panel' do
get 'users', to: 'panels#users', as: :panel_show_users
get 'photos', to: 'panels#photos', as: :panel_show_photos
end
Controller:
class PanelsController < ApplicationController
def users
#users = User.all
end
def photos
#photos = Photo.all
end
end
View file structure:
panels
|_ users.html.erb
|_ photos.html.erb
Okay, now I don't see any problems with that, to simply access the panels and populate the views with data. Do you see any problems?
Here is where I'm sort of at a cross roads though. What should I do when I want to Created Update and Delete a user/photo? Should I put them all in the PanelsController?
class PanelsController < ApplicationController
before_action :protect
def users
#users = User.all
end
def update_user
#user = User.find(params[:id])
#user.update(user_params)
end
def photos
#photos = Photo.all
end
def update_photo
#photo = Photo.find(params[:id])
#photo.update(photo_params)
end
private
def protect
redirect_to root_url, error: 'You lack privileges!'
end
end
While this would result in a large PanelsController, it would feel good to be able to execute that protect action and just one controller hook. It would also mean the routes would be easy to setup:
scope path: 'control_panel' do
get 'users', to: 'panels#users', as: :panel_show_users
post 'user', to: 'panels#update', as: :panel_create_user
get 'photos', to: 'panels#photos', as: :panel_show_photos
post 'photos', to: 'panels#photos', as: :panel_create_photo
end
I should use resource routes here?
Like I say, this will result in a huge panels controller, so I was thinking it may be better to have a separate controller for each resource and then redirect to panels views?
Routes:
scope path: 'control_panel' do
resources :users
resources :photos
end
Controllers:
class UsersController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def update
end
def destroy
end
end
class PhotosController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def update
end
def destroy
end
end
Still some quirks though. I have my Users#index action there, but what if I have two routes that return an index of all users? In the control panel, but also, when people are searching for another user, for example. Should I have two routes in the User controller? def public_users and def control_panel_users? That may be the answer. Could setup a hook to run #users = User.all in both of them, but redirect to a different location, and not have the protect method redirect them.
How should I protect these routes from non-admins? Should I move my protect method into the the application controller? Wouldn't this be a bit fiddly to setup?
class ApplicationController < ActionController
before_action :protect
def protect end
end
class StaticController < ApplicationController
skip_before_action [:home, :about, :contact]
def home
end
def about
end
def contact
end
end
But that is my question. 1 control panel controller or no control panel controller.
I really wish there was more advanced tutorials out there :( Billions of books on CRUD, MVC and things, but nothing on advanced things like control panels and AJAX...
Don't have a control panel controller. And to protect stuff from non-admins, use namespacing - read more about it here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
You can protect your 'admin'-namespaced controllers with authentication, and have the non-namespaced controllers open to the public (or open to non-admin users)
With regards to your def public_users and def control_panel_users question, you could just have two def index methods - one in the non-namespaced controller, and one in the admin-namespaced controller. They would each do different things.
So, you'd have 4 controllers in total:
2 non-namespaced, one for users, one for photos (containing all public stuff)
2 admin-namespaced, one for users, one for photos (containing all control panel stuff)
If you wanted, rather than using 'admin' as the namespace, you could use some other term you prefer - like 'panel'. 'Admin' is pretty conventional though.
I'm creating a new webpage were my users can have profile pages, and I can't seem to find the way to make a url like this:
webpage.com/profile/username
To go to my profile_controller action index and use the variable that comes on /username.
But I can't seem to find the way.
profile_controller.rb
class ProfileController < ApplicationController
def index
profile_info = Profile.find(params[:username])
end
end
And I've tried to work it around with the routes.rb but couldn't make it...
This route in your routes.rb should map get requests on /profiles/username to the index action of your profiles controller and pass the username value in params[:username]
get '/profiles/:username' => 'profiles#index'