另一个->形式中的第一个参数不能包含nil或为空

弗雷斯

如标题所示,我对微博有疑问。我真的不知道错误在哪里。关于此错误的其他帖子并没有真正解决我的问题,也没有给出解决方法的想法。

我正在尝试显示帖子并显示帖子创建框。

这是我所拥有的:

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)
阿曼·诺曼(Amr Noman)

您正在传递@micropost给,form_for但尚未在任何地方定义它,因此其值为nil

更改您的电话,使其form_for像这样:

<%= form_for(Micropost.new) do |f| %>

甚至更好,可以在控制器的操作中实例化它:

def memes
  @micropost = Micropost.new
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何理解形式的第一个参数在Rails控制器中不能包含nil或为空?

来自分类Dev

形式中的第一个参数不能包含nil或为空Hartl's Rails 4 Tutorial

来自分类Dev

格式中的第一个参数不能包含nil或为空-Rails 4

来自分类Dev

错误-表单中的第一个参数不能包含nil或为空

来自分类Dev

Rails获取错误:表单中的第一个参数不能包含nil或为空

来自分类Dev

ArguementError表单中的第一个参数不能包含nil或为空

来自分类Dev

Ruby on Rails:表单中的第一个参数不能包含nil或为空

来自分类Dev

Rails 4-表单中的第一个参数不能包含nil或为空

来自分类Dev

格式中的第一个参数不能包含nil或为空

来自分类Dev

Form_for的参数错误(表单中的第一个参数不能包含nil或为空)

来自分类Dev

Form_for的参数错误(表单中的第一个参数不能包含nil或为空)

来自分类Dev

ror4格式中的第一个参数不能包含nil或为空

来自分类Dev

格式中的第一个参数不能包含nil或为空Hartl's Rails 4 Tutorial

来自分类Dev

格式中的第一个参数不能包含nil或为空。form_for:method =>“ get”

来自分类Dev

设计用户更新表单 - 表单中的第一个参数不能包含 nil 或为空

来自分类Dev

形式中的第一个参数不能包含nil或在rails 4中为空

来自分类Dev

表单中的第一个参数不能包含nil或在渲染时为空

来自分类Dev

“表单中的第一个参数不能包含零或为空”,但它们(据我所知)不是零?

来自分类Dev

编辑操作错误(表单中的第一个参数不能包含 nil)

来自分类Dev

如何从另一个向量的另一个第一个元素减去向量中的第一个元素?

来自分类Dev

在具有泛型的另一个参数中重用第一个参数的键

来自分类Dev

PHP Show“ You”在另一个用户的最爱中的第一个

来自分类Dev

PHP Show“ You”在另一个用户的最爱中的第一个

来自分类Dev

选择另一个div中的第一个div

来自分类Dev

在另一个jbuilder模板中包含另一个

来自分类Dev

遍历包含另一个列表的列表,包含另一个列表的等

来自分类Dev

选择另一个元素内的第一个元素

来自分类Dev

如果第一个模式失败,则RegEx匹配另一个模式

来自分类Dev

用另一个变量的第一个单词设置变量

Related 相关文章

  1. 1

    如何理解形式的第一个参数在Rails控制器中不能包含nil或为空?

  2. 2

    形式中的第一个参数不能包含nil或为空Hartl's Rails 4 Tutorial

  3. 3

    格式中的第一个参数不能包含nil或为空-Rails 4

  4. 4

    错误-表单中的第一个参数不能包含nil或为空

  5. 5

    Rails获取错误:表单中的第一个参数不能包含nil或为空

  6. 6

    ArguementError表单中的第一个参数不能包含nil或为空

  7. 7

    Ruby on Rails:表单中的第一个参数不能包含nil或为空

  8. 8

    Rails 4-表单中的第一个参数不能包含nil或为空

  9. 9

    格式中的第一个参数不能包含nil或为空

  10. 10

    Form_for的参数错误(表单中的第一个参数不能包含nil或为空)

  11. 11

    Form_for的参数错误(表单中的第一个参数不能包含nil或为空)

  12. 12

    ror4格式中的第一个参数不能包含nil或为空

  13. 13

    格式中的第一个参数不能包含nil或为空Hartl's Rails 4 Tutorial

  14. 14

    格式中的第一个参数不能包含nil或为空。form_for:method =>“ get”

  15. 15

    设计用户更新表单 - 表单中的第一个参数不能包含 nil 或为空

  16. 16

    形式中的第一个参数不能包含nil或在rails 4中为空

  17. 17

    表单中的第一个参数不能包含nil或在渲染时为空

  18. 18

    “表单中的第一个参数不能包含零或为空”,但它们(据我所知)不是零?

  19. 19

    编辑操作错误(表单中的第一个参数不能包含 nil)

  20. 20

    如何从另一个向量的另一个第一个元素减去向量中的第一个元素?

  21. 21

    在具有泛型的另一个参数中重用第一个参数的键

  22. 22

    PHP Show“ You”在另一个用户的最爱中的第一个

  23. 23

    PHP Show“ You”在另一个用户的最爱中的第一个

  24. 24

    选择另一个div中的第一个div

  25. 25

    在另一个jbuilder模板中包含另一个

  26. 26

    遍历包含另一个列表的列表,包含另一个列表的等

  27. 27

    选择另一个元素内的第一个元素

  28. 28

    如果第一个模式失败,则RegEx匹配另一个模式

  29. 29

    用另一个变量的第一个单词设置变量

热门标签

归档