1

如标题所述,我制作了一个 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_many并使belongs_to应用程序工作。通过这种替换,可以访问Posts 属于UserUsers Post。我想知道是否可以在第一种情况下使用embeds_manyand进行关联embedded_in

4

0 回答 0