I am a newbie RoR developer. I am stuck in one problem. I am trying to make a application with users who can create a topic and others can post their comments under the topic..The topics are created by user and listed on the main page as a list. I am almost done with the topics. But when i click them to see the posts, I get Couldn’t find Topic without an ID error.
I guess that my mistake is topics has two indexes id and user_id.
here is my Topic model :
class Topic < ActiveRecord::Base
attr_accessible :content, :title
belongs_to :user
has_many :posts
validates :title, presence: true, length: { maximum: 140 }
validates :content, presence: true
validates :user_id, presence: true
default_scope order: 'topics.created_at DESC'
end
And my post model
class Posts < ActiveRecord::Base
attr_accessible :content
belongs_to :topic
belongs_to :user
validates :user_id, presence: true
validates :content, presence: true
default_scope order: 'posts.created_at DESC'
end
this is my topics_controller.rb:
class TopicsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def show
@topic = Topic.find(params[:id])
@posts = @topic.posts.paginate :page => params[:page], :per_page => 20
end
.
..vs
and this is my config/routes.rb
MyPedia::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :topics, only: [:show, :create, :destroy]
resources :posts
root to: 'static_pages#home'
match '/topicin', to: 'topics#show'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
my topics show.html.erb
<% title @topic.title %>
<div class ="center">
<h3><%= @topic.title %></h3>
<% if @posts? %>
<% for post in @posts do %>
<p><strong><%= post.content %>
<% end %>
<% end %>
</div>
I try to find a way to solve this problem.
thanks for helps
Try using GET /topic/1, that will help. The way you have set up the matched route is wrong and probably redundant. You can have a look here for more info: http://guides.rubyonrails.org/routing.html