2014-06-10 135 views
0

我試圖讓我的rails應用程序在通過設計註冊後重定向到edit_profile_path,但我在試圖解決問題時遇到嚴重問題。我在這裏花了四個小時試圖找到一個解決方案,但似乎無法找到問題。設計註冊後重定向到edit_profile

我目前的routes.rb:

resources :users, :only => [:index] do 
    member do 
     get :favourite_users, :favourited_users 
    end 
    resources :posts 
    resources :profiles, only: [:show, :edit] 
    end 

我RegistrationsController.rb

class RegistrationsController < Devise::RegistrationsController 

    protected 

    def after_sign_up_path_for(resource) 
    edit_profile_path(resource) 
    end 
end 

我的個人資料/ edit.html.erb:

class User < ActiveRecord::Base  
    before_create :build_profile #creates profile at user registration 

    has_one :profile, dependent: :destroy 
    accepts_nested_attributes_for :profile 

end 

我profile.rb

class Profile < ActiveRecord::Base 

    belongs_to :user 

    def name 
     first_name + " " + last_name[0] 
    end 
end 

我ProfilesController.rb

class ProfilesController < ApplicationController 

    def new 
     @profile = Profile.new 
    end 

    def show 
    end 

    def profile 
    end 

    def edit 
     @profile = current_user.profile 
    end 
end 

我試圖儘可能多的解決方案,我可以,但我似乎得到的是:

undefined method `profile_path' for #<#<Class:0xb50b8618>:0xb50abe54> 

,或者如果我從edit_profile_path刪除(資源) (資源)在註冊控制器:

No route matches {:action=>"edit", :controller=>"profiles"} missing required keys: [:id] 

任何幫助將不勝感激!

回答

0

您是否嘗試過運行從控制檯執行以下操作:

rake routes 

這將輸出所有您的應用程序所知道的路線。我希望我讀了你的路由文件的權利(很難看沒有格式化),但它是一個嵌套的路線,我覺得你真的想使用

edit_user_profile_path(resource) 
+0

我以前嘗試這樣做,我的一切是this:'No route matches {:action =>「edit」,:controller =>「profiles」,:user_id =>#<用戶ID:30,...:format => nil}缺少必需的鍵:[:id ]'。 Rake路線顯示我的路線是'edit_profile'(profiles#edit),因此我使用'edit_profile_path'。有什麼幫助嗎? –

+0

對不起,我最初讀錯了。你想讓用戶只有一個配置文件或多個配置文件?如果你想要一個用戶只有一個配置文件,你需要更新你的路線來說'資源配置文件',而不是'資源配置文件'。請參閱http://guides.rubyonrails.org/routing.html#singular-resources。 edit_user_profile_path(資源)應該在那之後工作。 – Keith

+0

啊太棒了!我還必須將profile.html.erb頂部行更改爲'<%= bootstrap_form_for @profile,url:edit_user_profile_path do | f | %>'閱讀了你添加的鏈接。現在起作用了,謝謝! –