ternggio
ternggio

Reputation: 19

Ruby on Rails: 'show' route of account

Currently I have two models, Account and User. Account has many users, and users belongs to account. I want to show account page but keep getting No route matches {:action=>"show", :controller=>"accounts"}, missing required keys: [:id] error.

Accounts controller

def show
   @account = Account.find_by(params[:id])
end

Links to account page

<%= link_to "Account Settings", account_path %>

Routes

resources :accounts, only: [:new, :create, :show, :edit, :update]

Upvotes: 1

Views: 91

Answers (2)

Abdelrahman Hassan
Abdelrahman Hassan

Reputation: 11

<%= link_to "Account Settings", account_path(@current_user.account_id) %>

Upvotes: 0

Andriy Kondzolko
Andriy Kondzolko

Reputation: 822

Problem here:

<%= link_to "Account Settings", account_path %>

Rails said 'missing required keys: [:id]' that means that you must send id to show the method

<%= link_to "Account Settings", account_path(123) %>

after when somebody will click on your link, the controller will take this id here:

@account = Account.find_by(params[:id])

will find your record into DB, and redirect you to :show page

Upvotes: 2

Related Questions