未定义的局部变量或方法`post'

时间:2015-09-29 00:14:16

标签: ruby-on-rails ruby posts

以下是我的评论表单_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 个答案:

答案 0 :(得分:1)

undefined local variable or method `post'

您的错误消息说明了一切。它告诉您,您没有在视图的相应控制器操作中定义post。这就是你得到这个错误的原因。

因此,您需要在相应的控制器操作中定义@post(通常在这种情况下使用实例变量),即PostsController的{​​{1}}操作。实际上,您需要定义show以及当前没有在控制器操作中使用它。因此,请更新您的@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

答案 1 :(得分:0)

Rails can pass instance variables from controllers to views, you should use @post replace post in conmments/_form.html.erb.

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