所有我必须提到的是我第一次在Rails中开发并且在我去的时候尝试学习。我使用此博客的指示来生成帖子和评论脚手架。我已经调整了一点以建立关系。但我试图将评论链接到帖子,我没有得到结果。我发布但不是在具体的帖子下....请帮助
我想要实现的目标:
Rails.application.routes.draw do
resources :commentonsqueals
resources :squeals
devise_for :users
resources :users do
get "users/show_image" => "users#show_image"
member do
get :following, :followers
end
end
用户控制器:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
has_many :comments
has_many :Squealingcampaign,dependent: :destroy #
has_many :campaign,dependent: :destroy #
has_many :followeds, through: :relationships
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id"
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
has_many:avatar, dependent: :destroy
has_many :posts, dependent: :destroy # remove a user's posts if his account is deleted.
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
class CommentonsquealsController < ApplicationController
before_action :set_commentonsqueal, only: [:show, :edit, :update, :destroy]
# GET /commentonsqueals
# GET /commentonsqueals.json
def index
@commentonsqueals = Commentonsqueal.all
end
# GET /commentonsqueals/1
# GET /commentonsqueals/1.json
def show
end
# GET /commentonsqueals/new
def new
@commentonsqueal = Commentonsqueal.new
end
# GET /commentonsqueals/1/edit
def edit
end
# POST /commentonsqueals
# POST /commentonsqueals.json
def create
@commentonsqueal = Commentonsqueal.new(commentonsqueal_params)
respond_to do |format|
if @commentonsqueal.save
format.html { redirect_to @commentonsqueal, notice: 'Commentonsqueal was successfully created.' }
format.json { render :show, status: :created, location: @commentonsqueal }
else
format.html { render :new }
format.json { render json: @commentonsqueal.errors, status: : :unprocessable_entity }
end
end
end
# PATCH/PUT /commentonsqueals/1
# PATCH/PUT /commentonsqueals/1.json
def update
respond_to do |format|
if @commentonsqueal.update(commentonsqueal_params)
format.html { redirect_to @commentonsqueal, notice: 'Commentonsqueal was successfully updated.' }
format.json { render :show, status: :ok, location: @commentonsqueal }
else
format.html { render :edit }
format.json { render json: @commentonsqueal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /commentonsqueals/1
# DELETE /commentonsqueals/1.json
def destroy
@commentonsqueal.destroy
respond_to do |format|
format.html { redirect_to commentonsqueals_url, notice: 'Commentonsqueal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_commentonsqueal
@commentonsqueal = Commentonsqueal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def commentonsqueal_params
params.require(:commentonsqueal).permit(:squeal_id, :body)
end
end
class Commentonsqueal < ActiveRecord::Base
belongs_to :post
end
<tbody>
<% @commentonsqueals.each do |commentonsqueal| %>
<tr>
<td><%= commentonsqueal.squeal_id %></td>
<td><%= commentonsqueal.body %></td>
<td><%= link_to 'Show', commentonsqueal %></td>
<td><%= link_to 'Edit', edit_commentonsqueal_path(commentonsqueal) %></td>
<td><%= link_to 'Destroy', commentonsqueal, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'New Commentonsqueal', new_commentonsqueal_path %>
当我尝试添加评论时,它只是不附加或在我发布的帖子下
答案 0 :(得分:0)
我找到了用于显示彼此相关的帖子的解决方案
def show
@comments = Commentonsqueal.all.where("squeal_id = ?",
Squeal.find_by_id(params[:id]))
end