PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] When a Post comment is made, no more can be created, only shown

 July 08, 2022     comments, posts, ruby, ruby-on-rails, ruby-on-rails-3.2     No comments   

Issue

I am trying to get a post and comment system working, for this however i want only one comment to be made per post. Only as i am trying to create a system as where content will be displayed followed by a comment 7 times in one post... Example...

program model 1 body content 1

Commentmodel1

program model 1 body content 2

Commentmodel2

program model 1 body content 3

Commentmodel3

.etc.etc.

For Me this is the simplest way of being able todo this by creating 7 different comment models, i know there is probably an easier way but as im new this seems the simplest. However i am struggling getting the one comment model to only allow just one comment to be made.

In this application coach is the user.

Here are the files involved, For the Models, program is the basic Post model, and comments is comments.

programs/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

In advance, thanks for your help, much appreciated!


Solution

Change your program comment relation to 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


Answered By - usha
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing