Rails路由错误嵌套路由(link_to user_post_path)

时间:2012-04-03 21:04:22

标签: ruby-on-rails routes nested

编辑//在底部添加了一个编辑。这些问题似乎与user_post_path有关。

我尝试输入:

<%= link_to 'show', user_post_path(post) %> 

但在编辑之前会收到下面列出的错误。

导致:

No route matches {:action=>"show", :controller=>"posts", :user_id=>#<Post id: 1, topic:
"1st Post", mood: "happy", post: "I don't know. Omg this works!", created_at: 
"2012-03-30   23:42:51", updated_at: "2012-04-01 06:01:48", user_id: 1>}

想法?


我查看了与此特定问题相关的其他帖子,但到目前为止我找不到解决问题的方法。

我的目标是显示与用户相关的帖子列表。 rake路由说/ users /:user_id / posts应该导致索引文件,但奇怪的是错误消息引用了post控制器中的show动作。为什么?另外,为什么哈希为:user_id为空?

感谢所有帮助。

以下是浏览器中的错误:

No route matches {:action=>"show", :controller=>"posts", :user_id=>#<Post id: 1, topic:
"1st Post", mood: "happy", post: "I don't know. Omg this works!", created_at: 
"2012-03-30   23:42:51", updated_at: "2012-04-01 06:01:48", user_id: 1>}

w ^

1 Testapp::Application.routes.draw do
2   resources :users do
3     resources :posts
4   end
5 
6   root to: 'home#index'

这是我的佣金路线:

user_posts GET    /users/:user_id/posts(.:format)          posts#index
           POST   /users/:user_id/posts(.:format)          posts#create
new_user_post GET    /users/:user_id/posts/new(.:format)      posts#new
edit_user_post GET    /users/:user_id/posts/:id/edit(.:format) posts#edit
user_post GET    /users/:user_id/posts/:id(.:format)      posts#show
          PUT    /users/:user_id/posts/:id(.:format)      posts#update
          DELETE /users/:user_id/posts/:id(.:format)      posts#destroy
      users GET    /users(.:format)                         users#index
           POST   /users(.:format)                         users#create
  new_user GET    /users/new(.:format)                     users#new
 edit_user GET    /users/:id/edit(.:format)                users#edit
      user GET    /users/:id(.:format)                     users#show
           PUT    /users/:id(.:format)                     users#update
           DELETE /users/:id(.:format)                     users#destroy
      root        /                                        home#index

帖子控制器:

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.find_by_user_id(params[:user_id]) 

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

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

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
   end

   # DELETE /posts/1
   # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

编辑//

我将问题缩小到link_to。

14 <% @posts.each do |post| %>
15   <tr>
16     <td><%= post.topic %></td>
17     <td><%= post.mood %></td>
18     <td><%= post.post %></td>
19     <td><%= post.user_id %></td>
20     <td><%= link_to user_post_path(post) %>  #this is giving me an error
21   </tr>
22 <% end %>
23 </table>

1 个答案:

答案 0 :(得分:3)

由于这些是嵌套路径,您是否考虑过传递用户以及帖子?您的最终网址需要user_id以及post_id,因此您可能需要拨打以下电话:

<%= link_to user_post_path(post.user, post) %> 

文档在这里:Rails Guide on Nested Resources