【问题标题】:When a Post comment is made, no more can be created, only shown发表评论后,无法再创建,仅显示
【发布时间】:2013-06-24 23:02:53
【问题描述】:

我正在尝试让帖子和评论系统正常工作,但是我希望每个帖子只发表一条评论。仅当我试图创建一个系统时,将在一个帖子中显示内容后跟评论 7 次...示例...

程序模型1正文内容1

评论模型1

程序模型1正文内容2

评论模型2

程序模型1正文内容3

评论模型3

.etc.etc.

对我来说,这是通过创建 7 个不同的评论模型来做到这一点的最简单方法,我知道可能有一种更简单的方法,但由于我是新手,这似乎是最简单的。但是,我正在努力让一个评论模型只允许发表一条评论。

在这个应用程序中,教练就是用户。

这里是涉及到的文件,对于Models,program是基本的Post模型,cmets就是cmets。

程序/Show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Title:</b><br />
  <%= @program.title %>
</p>

<p>
  <b>Body:</b><br />
  <%= @program.cweekcomments %>
</p>


<%= link_to 'Edit', edit_program_path(@program) %> |
<%= link_to 'Back', programs_path %>

<br /><br /><br />
<i>Comments</i>
<% @program.comments.each do |comment| %> 
  <p>
    <b>Comment:</b>

    <% if comment %>
        <%= comment.body %>
        <br />
        <%= link_to 'Edit', edit_program_comment_path(@program, comment) %> | <%= link_to 'Destroy', [@program, comment] , method: :delete, data: { confirm: 'Are you sure?' } %>
    <% else %>
        <%= form_for([@program, @program.comments.build]) do |f| %>
        <div class="field">
        <%= f.label :body %><br />
        <%= f.text_area :body %>
        </div>
        <div class="actions">
        <%= f.submit %>
        </div>
        <% end %>
    <% end %>
    </p>  
<% end %>

Programs_controller.rb

class ProgramsController < ApplicationController

  before_filter :authenticate_coach!, :except => [:show]


  # GET /programs
  # GET /programs.json

  def index
    @programs = Program.find_all_by_coach_id(current_coach[:id])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @programs }
    end
  end

  # GET /programs/1
  # GET /programs/1.json
  def show
    @program = Program.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @program }
    end
  end

  # GET /programs/new
  # GET /programs/new.json
  def new
    @program = Program.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @program }
    end
  end

  # GET /programs/1/edit
  def edit
    @program = Program.find(params[:id])
  end

  # POST /programs
  # POST /programs.json
  def create
    @program = Program.new(params[:program])


    respond_to do |format|
      if @program.save
        format.html { redirect_to @program, notice: 'Program was successfully created.' }
        format.json { render json: @program, status: :created, location: @program }
      else
        format.html { render action: "new" }
        format.json { render json: @program.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /programs/1
  # PUT /programs/1.json
  def update
    @program = Program.find(params[:id])

    respond_to do |format|
      if @program.update_attributes(params[:program])
        format.html { redirect_to @program, notice: 'Program was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @program.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /programs/1
  # DELETE /programs/1.json
  def destroy
    @program = Program.find(params[:id])
    @program.destroy

    respond_to do |format|
      format.html { redirect_to programs_url }
      format.json { head :no_content }
    end
  end
end

Comments_controller.rb

class CommentsController < ApplicationController

  def new
    @comment = @program.comments.build
  end 

  def create
    @program = Program.find(params[:program_id])
    @comment = @program.comments.create(params[:comment])
    redirect_to program_path(@program)
  end

  def destroy
    @program = Program.find(params[:program_id])
    @comment = @program.comments.find(params[:id])
    @comment.destroy
    redirect_to program_path(@program)
  end

  def edit
    @program = Program.find(params[:program_id])
    @comment = @program.comments.find(params[:id])
  end

   def update
    @program = Program.find(params[:program_id])
    @comment = @program.comments.find(params[:id])

    respond_to do |format|
      #if @program.comments.update_attributes(params[:comment])
      if @comment.update_attributes(params[:comment])
        format.html { redirect_to program_path(@program), notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
      end
    end

end

提前感谢您的帮助,非常感谢!

【问题讨论】:

  • 如果您只想为帖子添加一条评论,只需在CommentsController#create 中进行检查。如果@program 已有评论,请勿创建新评论。
  • 我该怎么做呢?抱歉,RoR 的新手

标签: ruby-on-rails ruby ruby-on-rails-3.2 comments posts


【解决方案1】:

将您的程序注释关系更改为 has_one.(has_one :comment in your program.rb)

def create
   @program = Program.find(params[:program_id])
   if @program.comment
     flash[:error] = "Cannot comment more than once"
   else
     @comment = @program.comments.create(params[:comment])
     flash[:notice] = "Comment created"
   end
   redirect_to program_path(@program)
end

【讨论】:

  • 非常感谢,当我更改为 has_one 时,我收到一个错误提示,在 我应该把它改成什么?
  • @program.comment 现在不返回数组。您不必遍历它。
  • 好的,这很酷,所以我现在将 更改为 现在没有返回错误,但是我现在如何构建创建评论的表格?目前我有 但它不喜欢它:(!
  • build 只能用于收藏。将其替换为@program.build_comment
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 2020-12-11
  • 2015-01-29
  • 2011-10-02
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多