2015-09-29 46 views
-1

這裏是我的我的意見_form.html.erb形式:未定義的局部變量或方法'後」

<%= form_for([post, @comment]) do |f| %> 
    <p> 
     <%= f.text_area :body, placeholder: "Write a comment!" %> 
    </p> 
    <br> 
    <p> <%= f.submit %> </p> 

<% end %> 

,這裏是我的職位形成_form.html.erb:

<%= form_for @post, url: post_path(@post), method: :patch do |f| %> 
     <% if @post.errors.any? %> 
      <div id="errors"> 
      <h2><%= pluralize(@post.errors.count, "Error") %> Prevent this post from posting</h2> 
      <ul> 
       <% @post.errors.full_messages.each do |msg| %> 
        <li><%= msg %></li> 

       <% end %> 
      </ul> 
      </div> 
     <% end %> 
     <p> 
      <%= f.label :Edit_Post %><br> 
      <%= f.text_area :body %> 
     </p> 
     <p> 
      <%= f.submit %> 
     </p> 

<% end %> 

這裏是我的comments_controller.rb:

class CommentsController < ApplicationController 
    def create 
     @post = Post.find(params[:post_id]) 
     @comment = @post.comments.build(params[:comment]) 
     @comment.save 
     redirect_to @posts 
    end 
    def destroy 
     @user = User.find(session[:user_id]) 
     @posts = Post.find(params[:post_id]) 
     @comment = @post.comments.find(params[:id]) 
     @comment.destroy 
     redirect_to post_path(@post) 
    end 
end 

這裏是我的posts_controller.rb:

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

    def index 
     @user = User.find(session[:user_id]) 
     @posts = Post.all 
    end 
    def welcome 
     @user = User.find(session[:user_id]) 
     @posts = Post.order("created_at desc").limit(4).offset(1) 
     @signed_in_user = session[:user_id] 
    end 
    def posts 
     @user = User.find(session[:user_id]) unless session[:user_id] == nil 
     redirect_to login_path, notice: "You're not logged in" unless @user 
     @signed_in_user = session[:user_id] 

    end 
    def new 
     @post = Post.new 
     @user = User.find(session[:user_id]) 
    end 
    def create 
     @user = User.find(session[:user_id])   
     @post = Post.new(post_params) 

     @post.user_id = @signed_in_user 

     if @post.save 
      redirect_to dashboard_path 
     else 
      render 'new' 
     end 
    end 
    def show 
     @user = User.find(session[:user_id]) 

     @signed_in_user = session[:user_id] 
    end 
    def edit 
     @user = User.find(session[:user_id]) 
    end 

    def update 
     if @post.update(post_params) 
      redirect_to @post, notice: "Your post has been updated!" 
     end 
    end 
    def destroy 
     @user = User.find(session[:user_id]) 
     @post.user_id = @signed_in_user 

     @post.destroy 

     redirect_to posts_path 
    end 

    private 

    def load_post 
     @post = Post.find(params[:id]) 
    end 
    def post_params 
     params.require(:post).permit(:body) 
    end 
end 

有此問題。任何幫助將不勝感激。這裏是我有錯誤的圖片:

enter image description here

編輯:這是我的日誌

Started GET "/posts/24" for 127.0.0.1 at 2015-09-28 18:46:44 -0700 
Processing by PostsController#show as HTML 
    Parameters: {"id"=>"24"} 
    [1m[36mPost Load (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", 24]] 
    [1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
    [1m[36mCACHE (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", "24"]] 
    [1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 24]] 
    Rendered comments/_form.html.erb (1.0ms) 
    Rendered posts/show.html.erb within layouts/application (7.0ms) 
    Rendered layouts/_nav.html.erb (1.0ms) 
Completed 200 OK in 451ms (Views: 412.0ms | ActiveRecord: 1.0ms) 

回答

2
undefined local variable or method `post' 

你的錯誤消息說明了一切。它告訴你,你的視圖沒有在相應的控制器動作中定義post。這就是爲什麼你得到這個錯誤。

因此,您需要在相應的控制器操作中定義@post(通常您在此類情況下使用實例變量),即PostsControllershow操作。您實際上需要定義@comment以及當前您的控制器操作中沒有它。因此,更新show方法是這樣的:

def show 
    @user = User.find(session[:user_id]) 
    @signed_in_user = session[:user_id] 

    # you need to define @post and @comment 
    @post = Post.find params[:id] 
    @comment = Comment.new(post: @post) 
end 

然後,您可以在視圖中使用@post@comment,所以你comments/_form.html.erb應該是這樣的:

<%= form_for([@post, @comment]) do |f| %> 
. . . 
. . . 
+0

好的,我知道你要去哪裏了,這很有意義,除非我在做出這些更改後出現以下問題。 找不到帖子'id'= – genobambino

+0

是的,你可能需要做一些調整來獲得'@ post'和'@ comment',這個想法是讓他們在控制器的action方法中正確定義。 「params」哈希如何看起來像?你可以這樣做:'將params.inspect'放入你的'show method'中嗎?您可能需要這樣做:'@post = Post.find params [:id]' –

+0

將@post更改爲您所說的內容後,我應該得到以下有趣的錯誤。 沒有路線匹配{:action =>「index」,:controller =>「comments」,:post_id => nil}缺少必需的鍵:[:post_id] 模板包含跟蹤:app/views/users/dashboard.html .erb – genobambino

0

Rails可以從控制器通過實例變量的觀點,你應該使用@post在更換postconmments/_form.html.erb

<%= form_for([@post, @comment = @post.comments.build]) do |f| %> 
+0

這樣做,我得到以下錯誤後 表單中的第一個參數不能包含零或爲空 – genobambino

相關問題