简体   繁体   English

Rails 4-嵌套资源的路由

[英]Rails 4 - routes for nested resource

I am trying to make a navbar in my rails 4 app. 我正在尝试在Rails 4应用程序中创建一个导航栏。

When a user registers, I want to automatically build a profile for that user. 用户注册后,我想自动为该用户建立配置文件。

I have models for user and profile. 我有用于用户和个人资料的模型。 The associations are: 关联是:

user has_one profile
profile belongs_to user

I'm struggling with the automatic build, but I have a different question now. 我正在为自动构建而苦苦挣扎,但是现在我有一个不同的问题。

In my navbar, I have a link to the users profile. 在我的导航栏中,我有一个指向用户个人资料的链接。 I'm trying to figure out how to get that link to work. 我试图弄清楚如何使该链接正常工作。

I have routes as follows: 我的路线如下:

 resources :users do
     resources :profiles
  end

I have also tried my routes using profile (singular), as: 我还使用配置文件(单数)尝试了我的路线,如下所示:

resources :users do
    resources :profile
end

In my navbar, I have: 在我的导航栏中,我有:

<% 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 %>

I have also tried without the '@' symbols, as: 我也尝试了不使用'@'符号,如下所示:

Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(current_user, profile)) %></span>

I have also tried: 我也尝试过:

Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>

To me, this appears consistent with the rails guide example: http://guides.rubyonrails.org/routing.html 对我来说,这似乎与rails指南示例一致: http : //guides.rubyonrails.org/routing.html

When I try this, I try creating a new user, but I get this error message: 尝试此操作时,我尝试创建一个新用户,但收到以下错误消息:

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: 

When I try the navbar path as: 当我尝试导航栏路径为:

Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@current_user, @profile) %>

I get this error: 我收到此错误:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil, :user_id=>nil} missing required keys: [:id, :user_id]):

Can anyone see how to set this up properly so the navbar links to the current users profile show page? 谁能看到如何正确设置此设置,以便导航栏链接到当前用户个人资料显示页面吗?

When I rake routes for grep profile, I get: 当我耙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

ANOTHER ATTEMPT 另一个尝试

When I try changing the nav bar link to: 当我尝试将导航栏链接更改为:

Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user, @profile) %></span>

I get the same error as I set out above. 我收到与上述相同的错误。

ANOTHER ATTEMPT 另一个尝试

When I try changing the nav bar link to: 当我尝试将导航栏链接更改为:

Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user_id, @profile_id) %></span>

I get the same error as I set out above. 我收到与上述相同的错误。

ANOTHER ATTEMPT 另一个尝试

When I try changing my routes to: 当我尝试将路线更改为:

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]

And changing my nav bar link to: 并将我的导航栏链接更改为:

 Hi <%= link_to 'current_user.first_name.titlecase', profile_path(@profile) %>

I get this error: 我收到此错误:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]):

When I try the nav bar link as shown in the suggestion below: 当我尝试如下所示的导航栏链接时:

Hi <%= link_to (current_user.first_name.titlecase, profile_path(@profile)) %></span>

I get this error: 我收到此错误:

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

When I try changing the nav bar link to: 当我尝试将导航栏链接更改为:

            Hi <%= link_to ('current_user.first_name.titlecase', profile_path(@profile)) %></span>

I get another syntax error as: 我收到另一个语法错误,因为:

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]: ...                               ^):

ANOTHER ATTEMPT 另一个尝试

When I try following the ideas in this post: 当我尝试遵循本文中的想法时:

User profile pages with devise - Routing to show action 具有设计的用户个人资料页面-路由以显示操作

I change my navbar link to: 我将导航栏链接更改为:

        Hi <%= link_to 'current_user.first_name.titlecase', current_user_profile_path %></span>

I then get this error message: 然后,我收到此错误消息:

ActionView::Template::Error (undefined local variable or method `current_user_profile_path' for #<#<Class:0x007f4337749f60>:0x007f4337749358>):

ANOTHER ATTEMPT 另一个尝试

When I try changing my navbar link to: 当我尝试将导航栏链接更改为:

 Hi <%= link_to('current_user.first_name.titlecase', profile_path) %></span>

I then get this error message: 然后,我收到此错误消息:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:id]):

I'm going to go crazy trying to learn to code. 我会疯狂地尝试学习编码。

NEW RAKE ROUTES FOR PROFILE IS: 配置文件的新耙径是:

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

When I start it up and press new user, the logs show this get command: 当我启动它并按新用户时,日志显示此get命令:

Started GET "/users/sign_up"

I'd suggest only nesting some profile actions, like this: 我建议只嵌套一些配置文件操作,如下所示:

resources :users do
    resources :profiles, only: [:new, :create]
end

and then using top-level routes for the other actions: 然后将顶级路由用于其他操作:

resources :profiles, only: [:show, :edit, :update, :destroy]

If you have the current_user , you can pass just that user's profile to those actions instead of both. 如果您具有current_user ,则可以仅将该用户的profile传递给这些操作,而不能同时传递给这两个操作。 Of course, your path names will change and you'll have to change those in your views. 当然,您的路径名将更改,并且您必须在视图中进行更改。 (Something like Hi <%= link_to(current_user.first_name.titlecase, profile_path(@profile)) %> ). (类似Hi <%= link_to(current_user.first_name.titlecase, profile_path(@profile)) %> )。

I'm curious, though, why have what appears to be a link to the user lead to the user's profile? 不过,我很好奇,为什么似乎有指向用户的链接导致了用户的个人资料? Why not link to Users#show , using user_path(@current_user) or whatever your equivalent route is, and then just rendering the profile on the Users#show view? 为什么不使用user_path(@current_user)或任何等效路由链接到Users#show ,然后仅在Users#show视图上呈现配置文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM