2

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
7
  • There should be :constraints => { :user => /[^\/]+/ } Commented Mar 21, 2016 at 11:04
  • @YevgeniyAnfilofyev : thanks for your answer but like i said, i already tried this solution and it doesn't work. Commented Mar 21, 2016 at 13:04
  • Just in your code there is :constraints => { :id => /[^/]+/ } . Maybe you misspelled but check carefully /[^/]+/ vs /[^\/]+/ Commented Mar 21, 2016 at 13:07
  • I write this : get 'front/:id' => 'projects#show', :constraints => { :id => /[^\/]+/ } and after rake routes but it doesn't work... Commented Mar 21, 2016 at 13:13
  • You might want to reorder your routes.rb file. Write get 'front/profile' => 'front#profile' before get 'front/:id' => 'projects#show' Commented Mar 21, 2016 at 13:17

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

You might want to change your route to

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

1 Comment

@RohitJangrid: thanks again, but it doesn't change the '.' to '/' . But if i change it myself in the url, it works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.