title: RoR information needed in routes.rb
body: <p>My question is to learn a little bit more about resources and routing. I followed ruby on rails tutorial book and started to customize my application.   In my application , I wanna have a user sending a Post and others discuss about that topic by sending photos. I created users without any problem. In the home page , I wanna show all the users posts by date. The topics model is </p>
  
  <pre><code>class Topic &lt; 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
  </code></pre>
  
  <p>posts model is :</p>
  
  <pre><code>class Post &lt; 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
  </code></pre>
  
  <p>and migration is :</p>
  
  <pre><code>      create_table "topics", :force =&gt; true do |t|
          t.string   "title"
          t.text     "content"
          t.datetime "created_at", :null =&gt; false
          t.datetime "updated_at", :null =&gt; false
          t.integer  "user_id"
        end
  
    add_index "topics", ["user_id", "created_at"], :name =&gt; "index_topics_on_user_id_and_created_at"
  </code></pre>
  
  <p>my topics_controller : </p>
  
  <pre><code>class TopicsController &lt; 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 =&gt; params[:page], :per_page =&gt; 20
      end
  </code></pre>
  
  <p>this is my routes.rb:</p>
  
  <pre><code>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/:id',   to: 'topics#show'
  </code></pre>
  
  <p>my show.html.erb is</p>
  
  <pre><code>&lt;div class ="center"&gt;
  &lt;h3&gt;&lt;%= @topic.title %&gt;&lt;/h3&gt;  
  &lt;% if  @posts %&gt;
    &lt;% for post in @posts do %&gt;  
      &lt;p&gt;&lt;strong&gt;&lt;%= post.content %&gt;  
  
  &lt;% end %&gt;  
  &lt;/div&gt; 
  &lt;%end %&gt;
  </code></pre>
  
  <p>my rake routes table is like that:</p>
  
  <pre><code>     topics POST   /topics(.:format)         topics#create
        topic GET    /topics/:id(.:format)     topics#show
              DELETE /topics/:id(.:format)     topics#destroy
        posts GET    /posts(.:format)          posts#index
              POST   /posts(.:format)          posts#create
     new_post GET    /posts/new(.:format)      posts#new
    edit_post GET    /posts/:id/edit(.:format) posts#edit
         post GET    /posts/:id(.:format)      posts#show
              PUT    /posts/:id(.:format)      posts#update
              DELETE /posts/:id(.:format)      posts#destroy
  </code></pre>
  
  <p>.I can see my topics in the homepage as links list without any problem. When i click them i want them to show me topic and the posts inside it. 
   when i use localhost:3000 this is what i get now.</p>
  
  <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"topics", :id=&gt;#&lt;Topic id: nil, title: nil, content: nil, created_at: nil, updated_at: nil, user_id: 1&gt;}
  </code></pre>
  
  <p>when i use localhost:3000/topics/1</p>
  
  <pre><code>SQLite3::SQLException: no such column: posts.topic_id: SELECT  "posts".* FROM "posts"  WHERE "posts"."topic_id" = 1 ORDER BY posts.created_at DESC LIMIT 20 OFFSET 0
  </code></pre>
  
  <p>So I read the instructions , but i cannot find a way to use topics directly. Sorry for long question.This will help me to understand many questions about rails. </p>
  
  <p>Edit 1: I added topic_id to posts table ... the static_pages controller is : </p>
  
  <pre><code>class StaticPagesController &lt; ApplicationController
    def home
      @topic = current_user.topics.build if signed_in?
          @topics = Topic.paginate :page =&gt; params[:page], :per_page =&gt; 20
    end
  </code></pre>
  
  <p>and the mistake is here in _topics.html.erb :</p>
  
  <pre><code> &lt;% for topic in @topics do %&gt;  
  !!!!!  &lt;li&gt;&lt;%=link_to topic.title, topicin_path(@topic) %&gt;&lt;/li&gt;  
  
   &lt;%= will_paginate @topics %&gt;
  &lt;% end %&gt;  
  </code></pre>