2016-12-01 106 views
0

我收到這個錯誤,我不明白,因爲我已經在評論控制器中定義了方法,不是嗎?試圖創建評論。 (未定義方法的評論)

我越來越有些困惑,爲什麼它不工作。

評論控制器:

class CommentsController < ApplicationController 
    def create 
     @story = Story.find(params[:story_id]) 
     @comment = @story.comments.create(params[:comment].permit(:name, :body)) 
     redirect_to root_path 
    end 
end 

故事控制器:

class StoriesController < ApplicationController 
     before_action only: [:destroy, :show, :edit, :update] 


    def index 
     @stories = Story.order('created_at DESC') 
    end 

    def new 
     @story = current_user.stories.build 
    end 

    def create 
     @story = current_user.stories.build(story_params) 
     if @story.save 
      flash[:success] = "Your beautiful story has been added!" 
      redirect_to root_path 
     else 
      render 'new' 
     end 
    end 

    def edit 
     @story = Story.find(params[:id]) 
    end 

    def update 
     @story = Story.find(params[:id]) 
     if @story.update_attributes(params.require(:story).permit(:name, :description)) 
      flash[:success] = "More knowledge, more wisdom" 
      redirect_to root_path 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @story = Story.find(params[:id]) 
     if @story.destroy 
      flash[:success] = "I think you should have more confidence in your storytelling" 
      redirect_to root_path 
     else 
      flash[:error] = "Can't delete this story, sorry" 
     end 
    end 

    def show 
     @stories = Story.all 
    end 

    private 

    def story_params 
     params.require(:story).permit(:name, :description) 
    end 



    end 

Index.html.erb: 

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


    <h1>This is a list of posts</h1> 

    <table> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Description</th> 
       <th>User</th> 
       <th colspan="3"></th> 
      </tr> 
     </thead> 

     <tbody> 
      <% @stories.each do |story| %> 
      <tr> 
      <td><%= story.name %></td> 
      <td><%= story.description %></td> 
      <td><%= story.user.username %></td> 
      <td><%= link_to 'Show', story %></td> 
      <% if user_signed_in? %> 
      <td><%= link_to 'Edit', edit_story_path(story)%></td> 
      <td><%= link_to 'Destroy', story_path(story),method: :delete,data: { confirm: 'Are you sure?' } %></td> 
      <% end %> 
      </tr> 
     </tbody> 
    </table> 

     <h2><%= @story.comments.count %>Comments</h2> 
       <%= render @story.comments %> 
       <h3>Add a comment</h3> 
       <%= render 'comments/form' %> 

    <%= link_to 'New Story', new_story_path %> 

故事控制器:

class CommentsController < ApplicationController 
    before_action :set_comment, only: [:show, :edit, :update, :destroy] 

    # GET /comments 
    # GET /comments.json 
    def index 
    @comments = Comment.all 
    end 

    # GET /comments/1 
    # GET /comments/1.json 
    def show 
    @comments = @story.comments.all 
    @comment = @stroy.comments.build 
    end 

    # GET /comments/new 
    def new 
    @comment = Comment.new 
    end 

    # GET /comments/1/edit 
    def edit 
    end 

    # POST /comments 
    # POST /comments.json 
    def create 
    @story = Story.find(params[:story_id]) 
    @story.comments.create(comment_params) 
    end 


    # PATCH/PUT /comments/1 
    # PATCH/PUT /comments/1.json 
    def update 
    respond_to do |format| 
     if @comment.update(comment_params) 
     format.html { redirect_to @comment, notice: 'Comment was successfully updated.' } 
     format.json { render :show, status: :ok, location: @comment } 
     else 
     format.html { render :edit } 
     format.json { render json: @comment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /comments/1 
    # DELETE /comments/1.json 
    def destroy 
    @comment.destroy 
    respond_to do |format| 
     format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_comment 
     @comment = Comment.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def comment_params 
     params.require(:comment).permit(:user_name, :body, :story_id) 
    end 
end 
+0

你可以顯示堆棧跟蹤?查看錯誤發生的位置會很有幫助。 – moveson

+0

on'''Index.html.erb'''我看到@story,但無法在控制器,動作索引中看到它。 –

回答

0

你必須瞭解如何協會開始工作:

一個故事會有很多評論。

你必須在模型中定義的關聯:

class Story < ActiveRecord::Base 
    has_many :comments 
end 

class Comment < ActiveRecord::Base 
    belongs_to :story 
end 

然後在控制器中的方法將可用:

class CommentsController < ApplicationController 
    def create 
    @story = Story.find(params[:story_id]) 
    @story.comments.create(comment_params) 
    end 
end 

而在你app/stories/show.html.erb觀點:

<% @story.comments.each do |comment| %> 
    <%= comment.body #or the comment content method %> 
<% end %> 
+0

什麼是在索引視圖中渲染和列出註釋的最佳方式? – Benjamints

+0

已更新的答案。 – tebayoso

0

確保您已經運行了正確的遷移

rails g scaffold comment user_name:string body:text story:references 

現在

rake db:migrate 

在你的故事模式寫

has_many :comments 

而且在您的評論模式確保您有

belongs_to :story 

或你的故事控制器在秀方法有這個

@comments = @story.comments.all 
@comment = @stroy.comments.build 

現在在你的故事的形式表演的看法,這樣的事情

<h3>Comments</h3> 
<% @comments.each do |comment| %> 
    <div> 
    <p><%= comment.body %></p> 
    </div> 
<% end %> 
<%= render 'comments/form' %> 

在您的評論/ _form.html.erb添加

<%= f.hidden_field :story_id %> 

如果你想顯示在您的索引編輯你喜歡這樣

<tbody> 
      <% @stories.each do |story| %> 
      <tr> 
      <td><%= story.name %></td> 
      <td><%= story.description %></td> 
      <td><%= story.user.username %></td> 
      <td><%= link_to 'Show', story %></td> 
      <% if user_signed_in? %> 
      <td><%= link_to 'Edit', edit_story_path(story)%></td> 
      <td><%= link_to 'Destroy', story_path(story),method: :delete,data: { confirm: 'Are you sure?' } %></td> 
      <% story.comments.each do |c| %> 
       <%= c.body %> 
      <% end %> 
      <% end %> 
      </tr> 
     </tbody> 
+0

感謝您的回覆!我會試試看。我可以將「user_name」重命名爲腳手架的「用戶名」,還是會與設計發生衝突? – Benjamints

+0

你可以有任何東西,那些是你的評論模型中的列 –

+0

當我將上面的代碼粘貼到我的index.html.erb中時,我收到一個'ActionView :: Template :: Error(每個'爲'nil:NilClass'的未定義方法'')'視圖。 – Benjamints