0
編輯//在底部添加了一個編輯。這些問題似乎與user_post_path有關。Rails路由錯誤嵌套路由(link_to 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動作。爲什麼?另外,爲什麼hash_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
Posts控制器:
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>
我正要回答,因爲我以前的解決方案秒鐘發現你回答了! – cj3kim 2012-04-04 02:03:29
這是我的解決方案,它工作。 <%= link_to'show',user_post_path(post.user_id,post.id)%> – cj3kim 2012-04-04 02:04:58
感謝您的回答,順便說一句。 :] – cj3kim 2012-04-04 02:17:42