Rails 4 - Nested Resources -


user has_many tickets.

ticket belongs_to user (ticket.user_id)

routes.rb

resources :users   resources :tickets end 

rake routes

user_tickets /users/:user_id/tickets(.:format) tickets#index 

users/index.html.erb

<%= link_to("view user's tickets", user_tickets_path(user)) %> 

users_controller.rb

private def set_user   @user = user.find(params[:id])   @tickets = @user.tickets end 

tickets_controller.rb

def index   @search = ticket.search(params[:q])   @tickets = @search.result.paginate(:page => params[:page], :per_page => 25)   render 'shared/tickets.html.erb' end 

when hover on link, shows .../users/[the selected user's id]/tickets

when goes ticket/index page, shows tickets, not tickets selected user's id.

i'm pretty sure route incorrect, or may else entirely. appreciated.

edit

i think problem need call @tickets in tickets_controller/index method variety of ways, because want use view @tickets.all, @user.tickets, @facility.tickets, etc (to keep dry).

the same index list of tickets needs change, based on link whence came (whether comes user list, showing list of tickets user, or facility list, showing list of tickets facility). i'm doing horribly wrong somewhere.

possible solution try:

maybe need create custom routes, get 'users_tickets' => "users#users_tickets", put @tickets conditions in method , call shared/tickets.html.erb way.

sounds need step through association. did use

tickets_controller.rb

def index      @user = user.find(params[:user_id])      @tickets = @user.tickets end 

in controller? if doesn't help, can post controller code?


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -