I'm making a site with one controller "Projects" and i want to show all projects with to routes :
- /admin/projects/:id = /admin/projects/1 (works)
- /front/:id = /front.1 (doesn't work)
I have tried this
get 'front/:id' => 'projects#show', :constraints => { :id => /[^/]+/ }in route.rb but it doesn't work.
My files :
routes.rb
Rails.application.routes.draw do resources :users, path: '/admin/clients' get 'admin' => 'admin#dashbord' get 'admin/profile' get 'admin/settings' get 'admin/_admin_header' get 'front' => 'front#index' get 'front/profile' => 'front#profile' get 'front/:id' => 'projects#show' scope '/admin' do resources :projects do resources :pictures end end end
projects_controller.rb
layout 'adminApplication' before_action :set_project, only: [:show, :edit, :update, :destroy] def index @projects = Project.all end def show end def new @project = Project.new end def edit end def create @project = Project.new(project_params) respond_to do |format| if @project.save format.html { redirect_to @project, notice: 'Project was successfully created.' } format.json { render :show, status: :created, location: @project } else format.html { render :new } format.json { render json: @project.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @project.update(project_params) format.html { redirect_to @project, notice: 'Project was successfully updated.' } format.json { render :show, status: :ok, location: @project } else format.html { render :edit } format.json { render json: @project.errors, status: :unprocessable_entity } end end end def destroy @project.destroy respond_to do |format| format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' } format.json { head :no_content } end end private def set_project @project = Project.find(params[:id]) end def project_params params.require(:project).permit(:name, :date, :location, :integer) end end
front_controller.rb
def index @projects = Project.all render 'projects/index' end def show end def profile end end
in projects/index.html.erb
- link_to 'Show', project - link_to 'Show', front_path(project)
I already checked all similar questions.
Thanks for your help !
Kazei Design
Update
rake routes | grep front
:
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
:constraints => { :user => /[^\/]+/ }
– Yevgeniy Anfilofyev:constraints => { :id => /[^/]+/ }
. Maybe you misspelled but check carefully/[^/]+/
vs/[^\/]+/
– Yevgeniy Anfilofyevget 'front/:id' => 'projects#show', :constraints => { :id => /[^\/]+/ }
and afterrake routes
but it doesn't work... – kazeidesignroutes.rb
file. Writeget 'front/profile' => 'front#profile'
beforeget 'front/:id' => 'projects#show'
– Rohit Jangid