2013-06-27 49 views
0

我想獲得一篇文章和評論系統的工作,但爲此,我只想每個帖子發表一條評論。只有當我想創建一個系統,其中的內容將在一個職位...實例...當發表評論時,不能創建更多,只能顯示

程序模型1個的主體內容1

顯示這是一個註釋7倍Commentmodel1

程序模型1個的主體內容2

Commentmodel2

程序模型1b中ODY內容3

Commentmodel3

.etc.etc。

對我來說這是一個能夠TODO這通過創建7種不同型號的評論最簡單的方法,我知道有可能是一個更簡單的方法,但作爲即時通訊新的,這似乎是最簡單。不過,我正在努力讓一個評論模型只允許一個評論。

在這個應用程序教練是用戶。

下面是涉及的文件,對於模型,程序是基本的Post模型,評論是註釋。

程序/ 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 

在前進,感謝您的幫助,非常感謝!

+0

'。如果'@ program'已經有評論,請不要創建一個新評論。 – khustochka

+0

我會怎麼做呢?感謝RoR – user2278385

回答

0

改變你的程序註釋關係HAS_ONE:如果你想添加一個職位只有一個評論,只是做在`CommentsController#創建檢查(HAS_ONE在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 
+0

非常感謝,當我改變爲has_one時,我得到一個erorr回來說'未定義的方法'每個'在<%@ program.comment.each do | comment | %>我應該怎樣改變它? – user2278385

+0

@ program.comment現在不返回數組。您不必遍歷它。 – usha

+0

好吧,這很酷,所以我現在改變<%= comment.body%>到<%= @ program.comment.body%>現在沒有返回錯誤,但我現在如何構建表單來創建註釋?目前我有<%= form_for([@ program,@ program.comment.build])do | f | %>但它dosnt喜歡它:(! – user2278385

相關問題