mayoneQD
mayoneQD

Reputation: 61

Can't link to another page with user

I want link to another page with my user, but i have an error:

RuntimeError in WelcomeController#edit
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

In my current page

<%= link_to "edit", welcome_edit_path(@user) %>

thi is my controller

 def edit
   user = User.find_by_id(@user.id)
 end

in my page i want link to

<%= user.id %>

I know my problem is my @user was nil, but i dont know how to link to another page with my @user So, please! help me

this my routes file

  get "micropost/new"

  get "user/new"
  get "user/saved"

  get "post/new"
  get "post/show"

  get "welcome/index"
  get "welcome/sucess"
  get "welcome/edit"
  root :to => "welcome#index"
  get '/users/:id', :to => 'welcome#sucess', :as => "user"  

  match '/relations', to: 'relation#create', via: 'post'
  match '/relations/:id', to: 'relation#destroy', via: 'delete'
  resources :users
  resources :relations,  only: [:create, :destroy]
  resources :microposts

  match '/login', to: 'welcome#create', via: 'post'
  match '/logout' => 'welcome#destroy', as: :logout
  match '/create', to: 'micropost#create', via: 'post'
  match '/signup', to: 'user#signup', via: 'post'

Upvotes: 0

Views: 73

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51181

Your code in edit method makes no sense. First thing, you have to define your route properly:

get '/welcome/edit/:id', to: 'welcome#edit', as: 'welcome_edit'

Then, in the WelcomeController:

def edit
  @user = User.find(params[:id])
end

and the link to this page:

<%= link_to 'edit', welcome_edit_path(@user) %>

Upvotes: 2

Related Questions