如标题所示,我对微博有疑问。我真的不知道错误在哪里。关于此错误的其他帖子并没有真正解决我的问题,也没有给出解决方法的想法。
我正在尝试显示帖子并显示帖子创建框。
这是我所拥有的:
microposts_controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
secure_post = params.require(:micropost).permit(:content)
@micropost = current_user.microposts.build(secure_post)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
end
microposts_form.html.erb
<%= form_for (@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
memes.html.erb
<% if logged_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<section class="micropost_form">
<%= render 'shared/micropost_form' %>
</section>
</aside>
</div>
<% end %>
确切错误: localhost:3000 / memes
我知道表格中的某处有NIL-但是应该在哪里以及如何将其固定?
Rails服务器:
Started GET "/memes" for ::1 at 2015-12-07 22:46:48 +0000
ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by StaticPagesController#memes as HTML
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
(0.0ms) SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = ? [["user_id", 6]]
Rendered shared/_user_info.html.erb (312.2ms)
Rendered shared/_micropost_form.html.erb (3.5ms)
Rendered static_pages/memes.html.erb within layouts/application (439.6ms)
Completed 500 Internal Server Error in 464ms (ActiveRecord: 1.0ms)
ActionView::Template::Error (First argument in form cannot contain nil or be empty):
1: <%= form_for(@micropost) do |f| %>
2: <%= render 'shared/error_messages', object: f.object %>
3: <div class="field">
4: <%= f.text_area :content, placeholder: "Compose new micropost..." %>
app/views/shared/_micropost_form.html.erb:1:in `_app_views_shared__micropost_form_html_erb__1950492356_78981080'
app/views/static_pages/memes.html.erb:75:in `_app_views_static_pages_memes_html_erb___1961772234_79299460'
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (42.1ms)
您正在传递@micropost
给,form_for
但尚未在任何地方定义它,因此其值为nil
。
更改您的电话,使其form_for
像这样:
<%= form_for(Micropost.new) do |f| %>
甚至更好,可以在控制器的操作中实例化它:
def memes
@micropost = Micropost.new
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句