這裏是我的我的意見_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
有此問題。任何幫助將不勝感激。這裏是我有錯誤的圖片:
編輯:這是我的日誌
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)
好的,我知道你要去哪裏了,這很有意義,除非我在做出這些更改後出現以下問題。 找不到帖子'id'= – genobambino
是的,你可能需要做一些調整來獲得'@ post'和'@ comment',這個想法是讓他們在控制器的action方法中正確定義。 「params」哈希如何看起來像?你可以這樣做:'將params.inspect'放入你的'show method'中嗎?您可能需要這樣做:'@post = Post.find params [:id]' –
將@post更改爲您所說的內容後,我應該得到以下有趣的錯誤。 沒有路線匹配{:action =>「index」,:controller =>「comments」,:post_id => nil}缺少必需的鍵:[:post_id] 模板包含跟蹤:app/views/users/dashboard.html .erb – genobambino