Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails 4的索引页上实例化新对象_Ruby On Rails_Instantiation_Nomethoderror - Fatal编程技术网

Ruby on rails 在Rails 4的索引页上实例化新对象

Ruby on rails 在Rails 4的索引页上实例化新对象,ruby-on-rails,instantiation,nomethoderror,Ruby On Rails,Instantiation,Nomethoderror,简单的问题,我无法解决一些如何 我试着模仿这其中的前几个步骤。我有一个图片-模型,我试图在索引页上实例化这种对象。因此,我使用的是以下几行: index.erb.html <%= form_for Picture.new do |f| %> <%= f.label :image, "Upload" %> <%= f.file_field :image, multiple: true %> <% end %> 您的图片资源嵌套在房屋

简单的问题,我无法解决一些如何

我试着模仿这其中的前几个步骤。我有一个
图片
-模型,我试图在索引页上实例化这种对象。因此,我使用的是以下几行:

index.erb.html

<%= form_for Picture.new do |f| %>
    <%= f.label :image, "Upload" %>
    <%= f.file_field :image, multiple: true %>
<% end %>

您的
图片
资源嵌套在
房屋
资源中。没有路径允许您创建新的
图片
,而没有
房屋
来提供周围的上下文,因此如果您只提供
图片,则
表单_
无法自动生成URL。新建

你需要给它一栋房子和一张照片

通常,您的系统会执行以下操作:

form_for [@house, @house.pictures.new] do |f|
<%= form_for [@house, @house.pictures.build] do |f| %>
    <%= f.label :image, "Upload" %>
    <%= f.file_field :image, multiple: true %>
<% end %>

根据给定的路线信息,您实际上没有
图片\u路径
。您只有这些路由(如果执行
rake路由
):

这就是为什么你会犯这样的错误

您可以访问
房屋图片路径
,但不能访问
图片路径

要解决此问题,您必须使用
house\u pictures\u path
,并将
@house
@pictures
作为参数发送给它。大概是这样的:

form_for [@house, @house.pictures.new] do |f|
<%= form_for [@house, @house.pictures.build] do |f| %>
    <%= f.label :image, "Upload" %>
    <%= f.file_field :image, multiple: true %>
<% end %>


能否显示您的完整视图代码:
index.html.erb
?请同时显示您的routes.rb。它实际上都在那里,但格式可能不好。我会把这个修好的。是的,就是这样。谢谢你再次睁开我的眼睛。尽管它必须是
@house.pictures.build
,因为它是一对多关联。如果你在回答中改变了这一点,我很乐意接受它作为解决问题的答案。我已经更新了我的答案。但是,你真的试过house.pictures.new了吗?这也应该起作用,因为它与图片有很多关联,所以
@house.pictures.new
@house.pictures.build
应该具有相同的效果。看看这个作为参考:是的,你是对的。你在第一个答案中写了
@house.picture.new
,但我应该知道缺少的“s”是问题,而不是方法。所以这两个词都是正确的,只要它是复数。谢谢你的帮助,没问题。很高兴这有帮助:)
house_pictures GET    /houses/:house_id/pictures(.:format) pictures#index
        houses GET    /houses(.:format)                    houses#index
<%= form_for [@house, @house.pictures.build] do |f| %>
    <%= f.label :image, "Upload" %>
    <%= f.file_field :image, multiple: true %>
<% end %>