Rails - Enrutamiento a una página de usuario

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.

Mi controlador:

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

mi archivo de rutas:

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!

preguntado el 05 de mayo de 13 a las 15:05

1 Respuestas

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

rutas.rb:

devise_for :users

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

En sus puntos de vista:

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

contestado el 05 de mayo de 13 a las 15:05

That worked! Thank you so much. I've been reading dozens of routing tutorials, but I was just getting more confused. - Tony Tambe

Yes, it takes some practice with controllers, but the learning is fast, especially with tools like 'rake routes' - bombo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.