【问题标题】:RoR information needed in routes.rbroutes.rb 中需要的 RoR 信息
【发布时间】:2012-06-26 16:27:18
【问题描述】:

我的问题是更多地了解资源和路由。我跟着 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"

我的主题控制器:

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

这是我的路线.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 %>

我的 rake 路由表是这样的:

     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

所以我阅读了说明,但我找不到直接使用主题的方法。抱歉,问题太长了。这将帮助我理解许多关于导轨的问题。

编辑1:我在posts表中添加了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 %>  

【问题讨论】:

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


    【解决方案1】:

    首先,您需要在主页中查看您的link_to。这个link_to好像找不到用户的话题。

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

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

    祝你好运。

    【讨论】:

    • 我现在将尝试管理主题和帖子的关系。实际上我希望用户创建一个主题并写一篇关于它的帖子(标题和内容),以后其他用户只能通过帖子回答(内容)。
    【解决方案2】:

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

    您可以使用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 或任何关系时,您需要在数据库中分配关系值,因此当您在帖子 ID 1 上并且希望它加载回复时,查询将搜索 post_id 等于 1 的回复。所以确保您运行迁移以添加此关系属性

    你好,

    【讨论】:

    • 我开始重新编码每个迁移。我将尝试用关系进行干净的编码。
    • 我不建议重写现有迁移,而是添加或删除新属性,例如关系列,否则您可能会迷失方向并卡在某个地方(根据经验)只需进行新迁移以调整您的数据库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多