Rails 4应用程序中的关联错误与mongoid + simple_form + devise

时间:2016-07-29 17:01:47

标签: ruby-on-rails ruby-on-rails-4 devise simple-form mongoid5

正如标题所述,我制作了一个包含

的Rails 4应用程序
  • mongoid(5.1.3)
  • 设计(4.2.0)
  • simple_form(3.2.1)

我使用以下命令设置我的应用程序:

rails g mongoid:config
rails g simple_form:install --bootstrap
rails g devise:install
rails g devise User
rails g scaffold post title content:text user:references

我在user.rb模型文件中添加以下代码,以便在两个模型之间建立关联:

  embeds_many :posts

当我运行应用并在其上创建用户时,我访问http://localhost:3000/posts/new以创建新帖子,我收到以下错误:

Started GET "/posts/new" for 127.0.0.1 at 2016-07-29 19:54:21 +0300
Processing by PostsController#new as HTML
MONGODB | Adding localhost:27017 to the cluster.
MONGODB | localhost:27017 | mongoblog_development.find | STARTED | {"find"=>"users", "filter"=>{}}
MONGODB | localhost:27017 | mongoblog_development.find | SUCCEEDED | 0.0009257450000000001s
  Rendered posts/_form.html.erb (134.4ms)
  Rendered posts/new.html.erb within layouts/application (140.6ms)
Completed 500 Internal Server Error in 144ms

ActionView::Template::Error (undefined method `user_ids' for #<Post _id: 579b8a3d419a8f180afbb798, title: nil, content: nil>
Did you mean?  user?):
    4:   <div class="form-inputs">
    5:     <%= f.input :title %>
    6:     <%= f.input :content %>
    7:     <%= f.association :user %>
    8:   </div>
    9: 
   10:   <div class="form-actions">
  app/views/posts/_form.html.erb:7:in `block in _app_views_posts__form_html_erb__2731111319355307354_47423040086160'
  app/views/posts/_form.html.erb:1:in `_app_views_posts__form_html_erb__2731111319355307354_47423040086160'
  app/views/posts/new.html.erb:3:in `_app_views_posts_new_html_erb__3061427264044886497_70253725122920'

我在搜索引擎上搜索了这里,但是这个问题没有任何相关内容。我该如何解决?

临时解决方案

使用embeds_manyembedded_in替换模型文件中的has_manybelongs_to关联可使应用运行。通过此替换,可以访问属于PostUser s User的{​​{1}}。我想知道是否可以使用Postembeds_many在第一种情况下建立关联。

0 个答案:

没有答案