Rails 3 - 嵌套资源路由 - 一对一关系

发布于 2024-10-12 18:57:49 字数 1506 浏览 4 评论 0原文

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

<%= 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

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

Having some trouble with some nested resources routing. What I'm trying to do is link to a user's profile page for editing purposes. In my view it is written as:

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

Which errors out with:

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">}

In my routes.rb file, it looks like so:

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

I checked my Rake routes, and it gave me this as a valid option:

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

Which I am able to manually navigate to. For good measures, here's proof of my controller:

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

Anyway, I've been having some problem tracking this down. Any pointers would help. For my DB design Profiles belong to Users in a one-to-one relationship. I'm hoping it's just something newbish I'm not noticing a new set of eyes might help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-10-19 18:57:49

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

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

edit_user_profile_path(current_user, @profile)

现在,Rails 将使用第一个参数 (current_user)路由的 :user_id 部分,以及 :id 的第二个参数 (@profile)。

If you look closely at your route, you'll see that it expects both a :user_id and an :id. The latter, in this case, refers to the user profile.

In order to tell Rails that you want that particular profile, you'll have to specify both the user and the profile in your link, like this:

edit_user_profile_path(current_user, @profile)

Now, Rails will use the first argument (current_user) for the :user_id part of the route, and the second argument (@profile) for the :id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文