我希望与我的用户链接到另一个页面,但我有一个错误:
RuntimeError in WelcomeController#edit
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
在我当前的页面
<%= link_to "edit", welcome_edit_path(@user) %>
这是我的控制者
def edit
user = User.find_by_id(@user.id)
end
在我的页面中,我希望链接到
<%= user.id %>
我知道我的问题是我的@user是nil,但我不知道如何用@user链接到另一个页面 所以,请!帮助我
这是我的路线文件
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'
答案 0 :(得分:2)
edit
方法中的代码毫无意义。首先,您必须正确定义路线:
get '/welcome/edit/:id', to: 'welcome#edit', as: 'welcome_edit'
然后,在WelcomeController
:
def edit
@user = User.find(params[:id])
end
以及此页面的链接:
<%= link_to 'edit', welcome_edit_path(@user) %>