Ruby on rails 如何编辑配置文件?(使用Desive并具有模型配置文件,但编辑方法存在错误)

Ruby on rails 如何编辑配置文件?(使用Desive并具有模型配置文件,但编辑方法存在错误),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在使用Desive并创建了一个模型配置文件 我已经将has_one关系添加到user,并属于:user-to-profile模型。我还在概要文件中添加了user_id列 我还补充说 before_filter :create_profile def create_profile profile = Profile.create end 在用户模型中。当一个新用户注册它时,就会创建一个新的概要文件do。因此,如果用户id为10,这就是在概要文件中创建的内容 #<Profile i

我正在使用Desive并创建了一个模型配置文件

我已经将has_one关系添加到user,并属于:user-to-profile模型。我还在概要文件中添加了user_id列

我还补充说

before_filter :create_profile
def create_profile 
  profile = Profile.create
end
在用户模型中。当一个新用户注册它时,就会创建一个新的概要文件do。因此,如果用户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> 
因此,它不是搜索id=2,而是查看用户的id,如何解决这个问题?我肯定我错过了一些非常小的东西,但不确定是什么

编辑-错误来自编辑方法中的此行@profile=profile.find(params[:id])

# profiles_controller.rb
def edit
  @profile = Profile.find(params[:id])
end
我尝试使用:user\u id和:profile\u id,但也出现了错误“找不到没有id的配置文件”

编辑2-

routes.rb有,它几乎是空白的

resources :profiles
rake路径输出(其他路径为设计标准路径)

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

不确定控制器的外观,但您可以明确告诉rails在哪里可以找到配置文件id。类似如下:

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

根据你原来的问题:

问题是,我在application.rb中放置了一个编辑链接

<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>
  • 但这是一个错误

    找不到id=10的配置文件

    因此,它不是搜索id=2,而是查看用户的id,如何解决这个问题

    我认为问题在于:

    `<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>`
    
    :id
    配置文件
    表的:id,而不是
    用户
    表-因此您的
    链接应该如下所示:

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

  • 您的控制器应该是这样的(它最初是如何工作的):


    这将为您的路由提供他们想要的信息。

    不确定您的控制器是什么样子,但您可以明确告诉rails在哪里可以找到配置文件id。如下所示:

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

    根据你原来的问题:

    问题是,我在application.rb中放置了一个编辑链接

    <li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>
    
  • 但这是一个错误

    找不到id=10的配置文件

    因此,它不是搜索id=2,而是查看用户的id,如何解决这个问题

    我认为问题在于:

    `<li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>`
    
    :id
    配置文件
    表的:id,而不是
    用户
    表-因此您的
    链接应该如下所示:

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

  • 您的控制器应该是这样的(它最初是如何工作的):


    这将为您的路线提供他们想要的。

    如果Desive已经提供了编辑功能,那么为什么您需要维护配置文件以进行编辑仍然如果您需要它,您不应该在配置文件中添加用户id列,它应该由用户提供

    试试这个:

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

    如果Desive已经提供了编辑功能,那么为什么您需要维护配置文件以进行编辑?如果您需要它,您不应该在配置文件中添加用户id列,它应该由用户提供

    试试这个:

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

    首先修复您的用户模型:

    before_filter :create_profile
    def create_profile 
      profile = Profile.create
    end
    
    应该是:

    after_create :create_profile
    
    def create_profile
      self.profile.create
    end
    
    before_过滤器适用于控制器,不适用于模型

    第二:改变这一行

    <li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>
    
    我认为您可以从profiles\u控制器中删除create操作,因为在创建概要文件的用户模型中有before\u create钩子

    你的路线

    resources :profiles
    
    只为编辑操作提供一个id参数,这是来自配置文件的id,而不是来自用户的id。您可以为当前用户添加一个通过嵌套路由的路由,但这不是必需的,因为您已经拥有designe中的current\u user方法,该方法返回您当前登录的用户

    顺便说一句:

    =>不是必需的

    "<h1>Profile</h1>".html_safe
    
    “Profile”.html\u安全
    

    =>这是必要的。

    首先修复您的用户模型:

    before_filter :create_profile
    def create_profile 
      profile = Profile.create
    end
    
    "<h1>Profile</h1>".html_safe
    
    应该是:

    after_create :create_profile
    
    def create_profile
      self.profile.create
    end
    
    before_过滤器适用于控制器,不适用于模型

    第二:改变这一行

    <li><%= link_to "Profile".html_safe, edit_profile_path(current_user) %></li>
    
    我认为您可以从profiles\u控制器中删除create操作,因为在创建概要文件的用户模型中有before\u create钩子

    你的路线

    resources :profiles
    
    只为编辑操作提供一个id参数,这是来自配置文件的id,而不是来自用户的id。您可以为当前用户添加一个通过嵌套路由的路由,但这不是必需的,因为您已经拥有designe中的current\u user方法,该方法返回您当前登录的用户

    顺便说一句:

    =>不是必需的

    "<h1>Profile</h1>".html_safe
    
    “Profile”.html\u安全
    

    =>这是必要的。

    您好,这将带我进入用户编辑表单,我需要转到配置文件编辑表单OK,然后在配置文件控制器中定义编辑方法,并按照dax答案查看视图。您尚未创建配置文件。首先创建一个新的配置文件我选中了,这个配置文件肯定是被创建的,虽然是的,用户id是零,所以我不得不通过rails控制台手动更改它。当所有的数据都存在时,在输入dax的代码后,我得到了错误“undefined method`profiles'for#”嗨,这将带我到用户编辑表单,我需要转到profile edit formok,然后在profile controller中定义编辑方法,并按照dax的答案查看。您还没有创建配置文件。首先创建一个新的配置文件我选中了,这个配置文件肯定是被创建的,虽然是的,用户id是零,所以我不得不通过rails控制台手动更改它。当所有的数据都存在时,在输入dax的代码之后,我得到了错误“undefined method`profiles',for#“我仍然有相同的错误,“找不到id为的概要文件”,错误出现在编辑方法(如上所示)@Profile=Profile.find(params[:id])的这一行,现在得到了这个错误“未定义#的#方法'profiles'”,我还尝试了当前的_user.profiles..f
    "<h1>Profile</h1>".html_safe