Rails 4 - 嵌套资源的路由
Rails 4 - routes for nested resource
我正在尝试在我的 rails 4 应用程序中制作导航栏。
当用户注册时,我想自动为该用户建立个人资料。
我有用户和配置文件的模型。这些协会是:
user has_one profile
profile belongs_to user
我正在为自动构建而苦苦挣扎,但我现在有一个不同的问题。
在我的导航栏中,我有一个 link 用户个人资料。我正在尝试弄清楚如何让 link 工作。
我有如下路线:
resources :users do
resources :profiles
end
我也尝试过使用个人资料(单数)的路线,如:
resources :users do
resources :profile
end
在我的导航栏中,我有:
<% if user_signed_in? %>
Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(@current_user, @profile)) %></span>
<span class="deviselinks" style="padding-right:30px">
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %></span>
<% else %>
我也试过不带'@'符号,如:
Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(current_user, profile)) %></span>
我也试过:
Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>
对我来说,这似乎与 rails 指南示例一致:http://guides.rubyonrails.org/routing.html
当我尝试此操作时,我尝试创建一个新用户,但我收到此错误消息:
ActionView::Template::Error (Nil location provided. Can't build URI.):
2015-12-28T21:08:51.228286+00:00 app[web.1]:
2015-12-28T21:08:51.228279+00:00 app[web.1]: 11: <ul style="text-align: right">
2015-12-28T21:08:51.228287+00:00 app[web.1]:
2015-12-28T21:08:51.228280+00:00 app[web.1]: 12: <span class="deviselinks" style="padding-right:30px">
2015-12-28T21:08:51.228281+00:00 app[web.1]: 13: <% if user_signed_in? %>
2015-12-28T21:08:51.228282+00:00 app[web.1]: 14: Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>
2015-12-28T21:08:51.228282+00:00 app[web.1]: 15:
当我尝试将导航栏路径设置为:
Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@current_user, @profile) %>
我收到这个错误:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil, :user_id=>nil} missing required keys: [:id, :user_id]):
任何人都可以看到如何正确设置它以便导航栏 link 显示当前用户个人资料页面吗?
当我为 grep 配置文件搜索路由时,我得到:
user_profiles GET /users/:user_id/profiles(.:format) profiles#index
POST /users/:user_id/profiles(.:format) profiles#create
new_user_profile GET /users/:user_id/profiles/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) profiles#edit
user_profile GET /users/:user_id/profiles/:id(.:format) profiles#show
PATCH /users/:user_id/profiles/:id(.:format) profiles#update
PUT /users/:user_id/profiles/:id(.:format) profiles#update
DELETE /users/:user_id/profiles/:id(.:format) profiles#destroy
另一次尝试
当我尝试将导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user, @profile) %></span>
我遇到了与上述相同的错误。
另一次尝试
当我尝试将导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user_id, @profile_id) %></span>
我遇到了与上述相同的错误。
另一次尝试
当我尝试将路线更改为:
devise_for :users, #class_name: 'FormUser',
:controllers => {
:registrations => "users/registrations",
# :omniauth_callbacks => "users/authentications"
:omniauth_callbacks => 'users/omniauth_callbacks'
}
# get '/auth/:provider/callback' => 'users/authentications#create'
# get '/authentications/sign_out', :to => 'users/authentications#destroy'
# PER SOURCEY TUTORIAL ----------
match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
resources :users do
resources :profiles, only: [:new, :create]
end
resources :profiles, only: [:show, :edit, :update, :destroy]
并将我的导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', profile_path(@profile) %>
我收到这个错误:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]):
当我尝试导航栏 link 时,如下面的建议所示:
Hi <%= link_to (current_user.first_name.titlecase, profile_path(@profile)) %></span>
我收到这个错误:
SyntaxError (/app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ',', expecting ')'
2015-12-28T22:14:54.726458+00:00 app[web.1]: ...rent_user.first_name.titlecase, profile_path(@profile)) );@o...
2015-12-28T22:14:54.726459+00:00 app[web.1]: ... ^
2015-12-28T22:14:54.726460+00:00 app[web.1]: /app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ')', expecting keyword_end
当我尝试将导航栏 link 更改为:
Hi <%= link_to ('current_user.first_name.titlecase', profile_path(@profile)) %></span>
我收到另一个语法错误:
SyntaxError (/app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ',', expecting ')'
2015-12-28T22:33:53.457021+00:00 app[web.1]:
2015-12-28T22:33:53.457014+00:00 app[web.1]: ...ent_user.first_name.titlecase', profile_path(@profile)) );@o...
2015-12-28T22:33:53.457015+00:00 app[web.1]: /app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ')', expecting keyword_end
2015-12-28T22:33:53.457015+00:00 app[web.1]: ... ^
2015-12-28T22:33:53.457016+00:00 app[web.1]: ...ase', profile_path(@profile)) );@output_buffer.safe_append='...
2015-12-28T22:33:53.457017+00:00 app[web.1]: ... ^):
另一次尝试
当我尝试遵循此 post 中的想法时:
User profile pages with devise - Routing to show action
我将导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', current_user_profile_path %></span>
然后我收到此错误消息:
ActionView::Template::Error (undefined local variable or method `current_user_profile_path' for #<#<Class:0x007f4337749f60>:0x007f4337749358>):
另一次尝试
当我尝试将导航栏 link 更改为:
Hi <%= link_to('current_user.first_name.titlecase', profile_path) %></span>
然后我收到此错误消息:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:id]):
我要疯狂地学习编码。
配置文件的新 RAKE 路线是:
edit_profile GET /profiles/:id/edit(.:format) profiles#edit
profile GET /profiles/:id(.:format) profiles#show
PATCH /profiles/:id(.:format) profiles#update
PUT /profiles/:id(.:format) profiles#update
DELETE /profiles/:id(.:format) profiles#destroy
user_profiles POST /users/:user_id/profiles(.:format) profiles#create
new_user_profile GET /users/:user_id/profiles/new(.:format) profiles#new
当我启动它并按新用户时,日志显示此获取命令:
Started GET "/users/sign_up"
我建议只嵌套一些配置文件操作,如下所示:
resources :users do
resources :profiles, only: [:new, :create]
end
然后使用 top-level 路由进行其他操作:
resources :profiles, only: [:show, :edit, :update, :destroy]
如果您有 current_user
,您可以只将该用户的 profile
传递给这些操作,而不是同时传递两者。当然,你的路径名会改变,你必须在你的视图中改变它们。 (类似于 Hi <%= link_to(current_user.first_name.titlecase, profile_path(@profile)) %>
)。
不过,我很好奇,为什么在用户看来 link 会导致用户的个人资料?为什么不 link 到 Users#show
,使用 user_path(@current_user)
或任何你的等效路线,然后只在 Users#show
视图上渲染配置文件?
我正在尝试在我的 rails 4 应用程序中制作导航栏。
当用户注册时,我想自动为该用户建立个人资料。
我有用户和配置文件的模型。这些协会是:
user has_one profile
profile belongs_to user
我正在为自动构建而苦苦挣扎,但我现在有一个不同的问题。
在我的导航栏中,我有一个 link 用户个人资料。我正在尝试弄清楚如何让 link 工作。
我有如下路线:
resources :users do
resources :profiles
end
我也尝试过使用个人资料(单数)的路线,如:
resources :users do
resources :profile
end
在我的导航栏中,我有:
<% if user_signed_in? %>
Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(@current_user, @profile)) %></span>
<span class="deviselinks" style="padding-right:30px">
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %></span>
<% else %>
我也试过不带'@'符号,如:
Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(current_user, profile)) %></span>
我也试过:
Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>
对我来说,这似乎与 rails 指南示例一致:http://guides.rubyonrails.org/routing.html
当我尝试此操作时,我尝试创建一个新用户,但我收到此错误消息:
ActionView::Template::Error (Nil location provided. Can't build URI.):
2015-12-28T21:08:51.228286+00:00 app[web.1]:
2015-12-28T21:08:51.228279+00:00 app[web.1]: 11: <ul style="text-align: right">
2015-12-28T21:08:51.228287+00:00 app[web.1]:
2015-12-28T21:08:51.228280+00:00 app[web.1]: 12: <span class="deviselinks" style="padding-right:30px">
2015-12-28T21:08:51.228281+00:00 app[web.1]: 13: <% if user_signed_in? %>
2015-12-28T21:08:51.228282+00:00 app[web.1]: 14: Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>
2015-12-28T21:08:51.228282+00:00 app[web.1]: 15:
当我尝试将导航栏路径设置为:
Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@current_user, @profile) %>
我收到这个错误:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil, :user_id=>nil} missing required keys: [:id, :user_id]):
任何人都可以看到如何正确设置它以便导航栏 link 显示当前用户个人资料页面吗?
当我为 grep 配置文件搜索路由时,我得到:
user_profiles GET /users/:user_id/profiles(.:format) profiles#index
POST /users/:user_id/profiles(.:format) profiles#create
new_user_profile GET /users/:user_id/profiles/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) profiles#edit
user_profile GET /users/:user_id/profiles/:id(.:format) profiles#show
PATCH /users/:user_id/profiles/:id(.:format) profiles#update
PUT /users/:user_id/profiles/:id(.:format) profiles#update
DELETE /users/:user_id/profiles/:id(.:format) profiles#destroy
另一次尝试
当我尝试将导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user, @profile) %></span>
我遇到了与上述相同的错误。
另一次尝试
当我尝试将导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user_id, @profile_id) %></span>
我遇到了与上述相同的错误。
另一次尝试
当我尝试将路线更改为:
devise_for :users, #class_name: 'FormUser',
:controllers => {
:registrations => "users/registrations",
# :omniauth_callbacks => "users/authentications"
:omniauth_callbacks => 'users/omniauth_callbacks'
}
# get '/auth/:provider/callback' => 'users/authentications#create'
# get '/authentications/sign_out', :to => 'users/authentications#destroy'
# PER SOURCEY TUTORIAL ----------
match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
resources :users do
resources :profiles, only: [:new, :create]
end
resources :profiles, only: [:show, :edit, :update, :destroy]
并将我的导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', profile_path(@profile) %>
我收到这个错误:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]):
当我尝试导航栏 link 时,如下面的建议所示:
Hi <%= link_to (current_user.first_name.titlecase, profile_path(@profile)) %></span>
我收到这个错误:
SyntaxError (/app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ',', expecting ')'
2015-12-28T22:14:54.726458+00:00 app[web.1]: ...rent_user.first_name.titlecase, profile_path(@profile)) );@o...
2015-12-28T22:14:54.726459+00:00 app[web.1]: ... ^
2015-12-28T22:14:54.726460+00:00 app[web.1]: /app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ')', expecting keyword_end
当我尝试将导航栏 link 更改为:
Hi <%= link_to ('current_user.first_name.titlecase', profile_path(@profile)) %></span>
我收到另一个语法错误:
SyntaxError (/app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ',', expecting ')'
2015-12-28T22:33:53.457021+00:00 app[web.1]:
2015-12-28T22:33:53.457014+00:00 app[web.1]: ...ent_user.first_name.titlecase', profile_path(@profile)) );@o...
2015-12-28T22:33:53.457015+00:00 app[web.1]: /app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ')', expecting keyword_end
2015-12-28T22:33:53.457015+00:00 app[web.1]: ... ^
2015-12-28T22:33:53.457016+00:00 app[web.1]: ...ase', profile_path(@profile)) );@output_buffer.safe_append='...
2015-12-28T22:33:53.457017+00:00 app[web.1]: ... ^):
另一次尝试
当我尝试遵循此 post 中的想法时:
User profile pages with devise - Routing to show action
我将导航栏 link 更改为:
Hi <%= link_to 'current_user.first_name.titlecase', current_user_profile_path %></span>
然后我收到此错误消息:
ActionView::Template::Error (undefined local variable or method `current_user_profile_path' for #<#<Class:0x007f4337749f60>:0x007f4337749358>):
另一次尝试
当我尝试将导航栏 link 更改为:
Hi <%= link_to('current_user.first_name.titlecase', profile_path) %></span>
然后我收到此错误消息:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:id]):
我要疯狂地学习编码。
配置文件的新 RAKE 路线是:
edit_profile GET /profiles/:id/edit(.:format) profiles#edit
profile GET /profiles/:id(.:format) profiles#show
PATCH /profiles/:id(.:format) profiles#update
PUT /profiles/:id(.:format) profiles#update
DELETE /profiles/:id(.:format) profiles#destroy
user_profiles POST /users/:user_id/profiles(.:format) profiles#create
new_user_profile GET /users/:user_id/profiles/new(.:format) profiles#new
当我启动它并按新用户时,日志显示此获取命令:
Started GET "/users/sign_up"
我建议只嵌套一些配置文件操作,如下所示:
resources :users do
resources :profiles, only: [:new, :create]
end
然后使用 top-level 路由进行其他操作:
resources :profiles, only: [:show, :edit, :update, :destroy]
如果您有 current_user
,您可以只将该用户的 profile
传递给这些操作,而不是同时传递两者。当然,你的路径名会改变,你必须在你的视图中改变它们。 (类似于 Hi <%= link_to(current_user.first_name.titlecase, profile_path(@profile)) %>
)。
不过,我很好奇,为什么在用户看来 link 会导致用户的个人资料?为什么不 link 到 Users#show
,使用 user_path(@current_user)
或任何你的等效路线,然后只在 Users#show
视图上渲染配置文件?