2

一些嵌套资源路由遇到问题。我想要做的是链接到用户的个人资料页面以进行编辑。在我看来,它写成:

<%= link_to "Edit Profile", edit_user_profile_path(current_user) %>

哪些错误:

No route matches {:action=>"edit", :controller=>"profiles", :user_id=>#<User id: 1, email: "EDITEDOUT", hashed_password: "EDITEDOUT", created_at: "2011-01-20 18:30:44", updated_at: "2011-01-20 18:30:44">}

在我的 routes.rb 文件中,它看起来像这样:

resources :users do
  resources :profiles, :controller => "profiles"
end  

我检查了我的 Rake 路线,它给了我一个有效的选项:

edit_user_profile GET    /users/:user_id/profiles/:id/edit(.:format)   {:action=>"edit", :controller=>"profiles"}

我可以手动导航到。为了采取好的措施,这是我的控制器的证明:

class ProfilesController < ApplicationController
  def edit
    @user = current_user
    @profile = current_user.profile
  end

  def update
    @user = current_user
    @profile = current_user.profile


    respond_to do |format|
      if @profile.update_attributes(params[:profile])
        format.html { redirect_to(orders_path, :notice => "Your profile has been updated.") }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end
end

无论如何,我一直在追踪这个问题。任何指针都会有所帮助。对于我的数据库设计,配置文件属于一对一关系的用户。我希望这只是一些新事物,我没有注意到一组新的眼睛可能会有所帮助。

4

1 回答 1

2

如果您仔细查看您的路线,您会发现它需要 a:user_id和 an :id。在这种情况下,后者指的是用户配置文件。

为了告诉 Rails 您需要该特定配置文件,您必须在链接中指定用户和配置文件,如下所示:

edit_user_profile_path(current_user, @profile)

现在,Rails 将使用第一个参数 ( current_user) 表示:user_id路由部分,第二个参数 ( @profile) 表示:id.

于 2011-01-20T23:27:14.507 回答