简体   繁体   中英

Rails - Routing to a user page

I have an app set up where a user has a profile page that shows all of the music reviews that they've posted to the site. I made a second page in the users controller that will have the same info, except sorted to only show the reviews from a certain year. (in this case the page name is tens2011) I want to place a link_to on the main show page to get to that 2011 page, but I'm having trouble with the routing.

My controller:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @pins = @user.pins.page(params[:page]).per_page(20)
  end

  def tens2011
    @user = User.find(params[:id])
    @pins = @user.pins.page(params[:page]).per_page(20)
  end
end

my routes file:

devise_for :users
match 'users/:id' => 'users#show', as: :user

get 'users/tens2011/:id' => 'users#tens2011'

I know that my routing is wrong, but I can't figure out how to get the link to be /users/tens2011/username

Any help would be appreciated for this newbie!

I invite you to read the guides to understand the details of routes with rails: http://guides.rubyonrails.org/routing.html

routes.rb :

devise_for :users

resources :users, only: [:show, :tens2011] do
  member do 
   get :show
   get :tens2011
  end
end

In your views :

link_to "2011", tens2011_user_path(@user)

For help you, you can check your routes with :

rake routes

A key to understanding the rules of routing is the different between a member resource and collection resource : http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM