0

编辑//在底部添加了一个编辑。这些问题似乎与 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>
4

1 回答 1

3

由于这些是嵌套路径,您是否考虑过传递用户和帖子?您的最终到达 URL 需要 auser_id和 a post_id,因此您可能需要调用以下命令:

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

文档在这里:Rails Guide on Nested Resources

于 2012-04-04T02:00:56.257 回答