routes.rb中需要的RoR信息

时间:2012-06-25 11:59:18

标签: ruby-on-rails rails-activerecord rails-routing

我的问题是要更多地了解资源和路由。我按照ruby on rails教程书开始定制我的应用程序。在我的应用程序中,我想让用户发送帖子,其他人通过发送照片来讨论该主题。我没有任何问题地创建了用户。在主页中,我想按日期显示所有用户的帖子。主题模型是

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
帖子模型是:

class Post < 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

和迁移是:

      create_table "topics", :force => true do |t|
        t.string   "title"
        t.text     "content"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
        t.integer  "user_id"
      end

  add_index "topics", ["user_id", "created_at"], :name => "index_topics_on_user_id_and_created_at"

my topics_controller:

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

这是我的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/:id',   to: 'topics#show'

我的show.html.erb是

<div class ="center">
<h3><%= @topic.title %></h3>  
<% if  @posts %>
  <% for post in @posts do %>  
    <p><strong><%= post.content %>  

<% end %>  
</div> 
<%end %>

我的佣金路线表是这样的:

     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

。我可以在主页上看到我的主题作为链接列表没有任何问题。当我点击它们时,我希望它们向我展示主题及其中的帖子。  当我使用localhost:3000时,这就是我现在所能得到的。

No route matches {:action=>"show", :controller=>"topics", :id=>#<Topic id: nil, title: nil, content: nil, created_at: nil, updated_at: nil, user_id: 1>}

当我使用localhost:3000 / topics / 1

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

所以我阅读了说明书,但我找不到直接使用主题的方法。很抱歉很长时间的问题。这将有助于我理解有关rails的许多问题。

编辑1:我将topic_id添加到帖子表... static_pages控制器是:

class StaticPagesController < ApplicationController
  def home
    @topic = current_user.topics.build if signed_in?
        @topics = Topic.paginate :page => params[:page], :per_page => 20
  end

,错误在_topics.html.erb:

 <% for topic in @topics do %>  
!!!!!  <li><%=link_to topic.title, topicin_path(@topic) %></li>  

 <%= will_paginate @topics %>
<% end %>  

2 个答案:

答案 0 :(得分:0)

首先,您需要在主页中查看link_to。似乎这个link_to无法找到用户的主题。

topic_id中还需要posts,因为belongs_to :topic模型中有Post。请查看posts的迁移,创建正确的迁移并rake db:migrate

最后,我不认为user需要主题,帖子有主题,而不是用户。然后,您可以调用@user.posts.topictopics.first.posts.first.user

祝你好运。

答案 1 :(得分:0)

因为您似乎正在使用嵌套属性,但您没有使用嵌套路由资源,因此路径生成每个主题的值而不是每个帖子的主题。

您可以使用resource do方法

轻松完成此操作
resource :posts do 
  resource :topics
end

或者像Wawa Loo说你需要传递关系值

也是因为你的错误 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

当您创建has_many或任何关系时,您需要在db中分配关系值,因此当您在帖子ID 1上并且您希望它加载回复时,查询将搜索post_id等于1的回复。确保运行迁移以添加此关系属性

问候,