2014-09-25 22 views
0

我創建了一個應用程序,其中用戶HAS_ONE輪廓和belongs_to的用戶配置文件。 它的設置使用戶創建時自動創建配置文件(在用戶控制器中)爲什麼我不能顯示編輯的形式?

我不想設置它,因此用戶可以訪問編輯頁面(配置文件控制器 - >編輯操作)和通過渲染的形式修改個人資料信息(使用_form部分)

我不明白爲什麼我不能讓編輯表單中顯示,據我可以告訴方法是否正確定義?任何人有任何想法爲什麼?非常感謝任何幫助!

瀏覽器:

NoMethodError in ProfilesController#edit 
undefined method `user' for nil:NilClass  

    def edit 
    @profile = @profile.user 
    end 

    def destroy 

資料控制器:

class ProfilesController < ApplicationController 
    before_action :logged_in_user, only: [:edit, :destroy] 

    def edit 
    @profile = @profile.user 
    end 

    def destroy 
    end 
end 

用戶控制器

class UsersController < ApplicationController 
    before_action :logged_in_user, only: [:edit, :update] 
    before_action :correct_user, only: [:edit, :update] 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     @user.profile = Profile.new 
     @user.send_activation_email 
     flash[:info] = "Please check your email to activate your account." 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

... 

組的意見/型材/ edit.html.erb

<% provide(:title, 'Edit Profile') %> 
<h1>Editing profile</h1> 

<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <%= form_for(@profile) do |f| %> 
     <%= render 'form', f: f %> 
     <%= f.submit "Edit Information", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 

的意見/型材/ _form.html.erb

<%= render 'shared/error_messages' %> 

<%= f.label :first_name %> 
<%= f.text_field :first_name, class: 'form-control' %> 

... 
+0

你使用設計來處理用戶身份驗證?如果你是(和恕我直言,你應該)你應該可能使用@profile = current_user.profile,與您目前的設置,我可能能夠編輯任何用戶配置文件,只需更改URL中的ID – Richlewis 2014-09-25 08:09:40

+0

謝謝!做到了!這是一個明顯的答案!我一整天都在編碼,所以我認爲我的大腦剛開始融化。我建立了我自己的身份驗證,所以我可以更好地瞭解工程─我不喜歡一切到底是怎樣設計,因爲我看不到自己的控制器和不喜歡不理解發生了什麼事繼承。 – sss333 2014-09-25 08:19:05

+0

我可以接受你的答案,如果你發佈它作爲一個?你是說我應該用色器件,因爲我還沒有建成就足夠阻止對某人潛在的編輯通過改變URL中的個人資料? – sss333 2014-09-25 08:27:35

回答

1

不知道您的身份驗證系統看起來像什麼,雖然你提到你看着裝置和後建自己的,所以我認爲你需要做這樣的事情在你的編輯操作

def edit 
    @profile = current_user.profile 
    end 
0

你想查看在user'profile而不是profile'users ,在ProfilesController中試試這個。

class ProfilesController < ApplicationController 
    before_action :logged_in_user, only: [:edit, :destroy] 

    def edit 
    @profile = current_user.profile 
    end 

    def destroy 
    end 
end 
+0

我試着第一次,它有同樣的問題 - 這就是爲什麼我試圖切換它 – sss333 2014-09-25 08:19:59

+0

是否使用設計寶石登錄?如果是,那麼你可以訪問current_user – LolWalid 2014-09-26 09:07:43

相關問題