Edit// Added an edit to the bottom. The issues seems to be linked to the user_post_path.
I tried typing:
<%= link_to 'show', user_post_path(post) %>
but get the error listed below before my edit.
Which led to:
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>}
Ideas?
I've looked at the other posts related to this particular issue, but I've found no solution for my problem so far.
What I'm aiming for is to show a list of posts associated with a user. The rake routes says that /users/:user_id/posts should lead to the index file but strangely the error message refers to the :show action in the post controllers. Why? Also, why is the hash for :user_id empty?
All help is appreciated.
Here is my error in the browser:
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'
Here is my rake routes:
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 controller:
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
Edit//
I scoped down the problem to a 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>