2013-08-17 36 views
0

我使用设计,并已创建模型配置文件。如何编辑配置文件? (使用设计,并有一个模型配置文件,但编辑方法是给错误)

我已将has_one关系添加到用户和belongs_to:用户到配置文件模型。我还在配置文件中添加了user_id列。

我也曾在用户模型中添加

before_filter :create_profile 
def create_profile 
    profile = Profile.create 
end 

。当新用户注册时,新的配置文件将被创建。因此,如果用户ID是10,这是在轮廓越来越创建

#<Profile id: 2, name: nil, created_at: "2013-08-17 06:44:33", updated_at: "2013-08-17 06:47:00", user_id: 10> 

现在的问题是,我已经把编辑链接application.rb中

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li> 

,但它给错误

Couldn't find Profile with id=10 

因此,而不是搜索id = 2,它正在查看user_id,如何解决这个问题?我相信我错过了一些非常小但不知道是什么的东西。

编辑 - 误差从该线到来@profile = Profile.find(PARAMS [:ID])在编辑方法

# profiles_controller.rb 
def edit 
    @profile = Profile.find(params[:id]) 
end 

我曾尝试使用:USER_ID和:PROFILE_ID但也给错误 “无法找到没有ID的个人资料”

EDIT 2 -

的routes.rb有,这是相当多的空白

resources :profiles 

耙路输出(其它路径是制定标准路径)

   profiles GET  /profiles(.:format)          profiles#index 
         POST  /profiles(.:format)          profiles#create 
      new_profile GET  /profiles/new(.:format)         profiles#new 
      edit_profile GET  /profiles/:id/edit(.:format)       profiles#edit 
       profile GET  /profiles/:id(.:format)         profiles#show 
         PUT  /profiles/:id(.:format)         profiles#update 
         DELETE  /profiles/:id(.:format)         profiles#destroy 

profiles_controller.rb外,其他5个方法是标准的一次

def edit 
    @user = current_user 
    @profile = @user.profiles.find(params[:profile_id]) 

    end 

    def create 

    @profile = current_user.build_profile(params[:profile]) 

    respond_to do |format| 
     if @profile.save 
     format.html { redirect_to @profile, notice: 'User profile was successfully created.' } 
     format.json { render json: @profile, status: :created, location: @profile } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @profile.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

回答

2

首先解决您的用户模型:

before_filter :create_profile 
def create_profile 
    profile = Profile.create 
end 

应该是:

after_create :create_profile 

def create_profile 
    self.profile.create 
end 

的before_filter是控制器,而不是模型!

二:这一行

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li> 

更改为这一个:

<li><%= link_to "Profile", edit_profile_path(current_user.profile) %></li> 

三:改变你的控制器编辑操作:

def edit 
    @profile = current_user.profile 
end 

我想你可以删除创建行动从你的profiles_controller中,因为你的用户模型中创建了before_create钩子个人资料。

你的路线

resources :profiles 

只是为您提供了编辑操作的id参数,并that's的ID从配置文件,而不是从用户。您可以通过嵌套路由为current_user添加一个,但这不是必需的,因为您已经拥有了设计中的current_user方法,它会返回当前登录的用户。

顺便说一句:

"Profile".html_safe 

=>是没有必要的。

"<h1>Profile</h1>".html_safe 

=>将是必要的。

+0

嗨,以及我得到这个错误“没有路线匹配{:行动=>”编辑“,:控制器=>”配置文件“,:id =>零)”,我在application.html中使用edit_profile_path(@profile) .erb,耙路也显示 - edit_profile GET /profiles/:id/edit(.:format)profiles#编辑 所以路径也存在 – iCyborg

+0

将@profile更改为current_user.profile(您应该设置at-profile实例变量在控制器操作中,它被调用以显示包含编辑链接的页面)。如果current_user.profile什么也没有返回,你的用户还没有任何配置文件.. – Mattherick

+0

抱歉无法得到它,我应该在profiles_controller.rb中编辑什么?我应该在哪里设置它?我认为我们几乎在那里 – iCyborg

1

不知道你的控制器看起来像,但你可以明确地告诉rails哪里可以找到profile_id。是这样的:

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user, profile_id: @profile) %></li> 

编辑
#<Class:0xb459b8c>是整个用户类,而不是一个实例这就是为什么你不能把它profiles。试试这个:

@user = current_user 
@profile = @user.profiles.find(params[:profile_id]) 

编辑2

根据你原来的问题:

的问题是,我已经把应用程序的编辑链接。RB

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>

,但它给错误

Couldn't find Profile with id=10

所以不是搜索ID = 2,它正在寻找USER_ID,如何解决这一问题?

的问题,我认为,是在这里:

`<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>` 

你的`edit_profile_path”呼吁

/profiles/:id/edit(.:format) 

:id是:在profile表的ID,而不是user表 - 所以你的link_to应该看起来像这样:

<li><%= link_to "Profile".html_safe, edit_profile_path(id: @profile.id) %></li>

或只是

<li><%= link_to "Profile".html_safe, edit_profile_path(@profile) %></li>

和控制器应该是这样的(它是怎么做最初):

# profiles_controller.rb 
def edit 
    @profile = Profile.find(params[:id]) 
end 

,这将给你的路由他们想要的东西。

+0

我仍然有同样的错误,“无法找到带有ID的配置文件”,错误是在编辑方法中的这一行(如上所示) @profile = Profile.find(params [:id]) – iCyborg

+0

现在得到这个错误“无法找到没有ID的配置文件” – iCyborg

+0

现在得到这个错误“未定义的方法配置文件”为#“,我也试过current_user.profiles..find(params [:profile_id ])和user.profiles.find(params [:profile_id]),仍然是相同的错误。我检查了轨道控制台和所有的数据是在那里,所以唯一的问题是我们没有正确的语法来调用编辑 – iCyborg

2

如果设计已经提供了编辑功能,那么为什么你需要维护配置文件,如果你需要它,你不应该在配置文件中添加user_id列,它应该来自用户。

试试这个:

<%= link_to "edit", edit_user_registration_path(current_user) %> 
+0

嗨,好吧,这是带我到用户编辑表单,我需要去个人资料编辑表格 – iCyborg

+0

好,然后定义一个在配置文件控制器中编辑的方法,并遵循dax答案的意见。 – Sami

+0

现在出现此错误“找不到没有ID的配置文件” – iCyborg