Rails Dot in Url with two routes for one controller

kazeidesign Source

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
ruby-on-railsrubyroutes

Answers

answered 3 years ago Rohit Jangid #1

You might want to change your route to

get 'front.:id' => 'projects#show'

answered 3 years ago Vucko #2

You're using a named route helper, but you didn't specify it:

- link_to 'Show', front_path(project)

And you can see in your routes that front_path for projects#show doesn't exist:

rake routes | grep front
           front    GET    /front(.:format)                            front#index
   front_profile    GET    /front/profile(.:format)                    front#profile
                    GET    /front/:id(.:format)                        projects#show

So, in your routes.rb add the helper:

get 'front/:id' => 'projects#show', as: :custom_front

And now run rake routes to see the new helper (it should be custom_front_path) and use it:

- link_to 'Show', custom_front_path(project)

See more info in the documentation - 4.3 Overriding the Named Helpers

comments powered by Disqus