Ruby on rails 4 e_id,:角色,:_销毁, 场馆属性:[:id,:name, 图片_属性:[:图像]]) 结束 结束
以下是我的看法:Ruby on rails 4 e_id,:角色,:_销毁, 场馆属性:[:id,:name, 图片_属性:[:图像]]) 结束 结束,ruby-on-rails-4,paperclip,polymorphic-associations,strong-parameters,Ruby On Rails 4,Paperclip,Polymorphic Associations,Strong Parameters,以下是我的看法: The Events form: <%= form_for @event, html: { multipart: true } do |f| %> <%= render 'shared/error_messages', object: f.object %> <fieldset id="event-meta"> <div class="form-group"> <%= f.label :title %>
The Events form:
<%= form_for @event, html: { multipart: true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<fieldset id="event-meta">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, rows: 8, class: "form-control" %>
<br />
</div>
</fieldset>
<div class="form-group">
<p>Upload Picture</p>
<%= f.fields_for :picture do |image_upload| %>
<%= image_upload.file_field :image, class: "form-control" %>
<% end %>
</div>
....
The Artist form:
<%= form_for @artist, html: { multipart: true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
</div>
<div class="form-group">
<p>Upload Picture</p>
<%= f.fields_for :picture do |image_upload| %>
<%= image_upload.file_field :image, class: "form-control" %>
<% end %>
</div>
事件形式:
上传图片
....
艺术家形式:
上传图片
所以…在我看来,一切都应该正常…但事实并非如此。我可能错过了一些简单的东西,希望有人能为我指出
FWIW…昨晚在搜索答案的时候,我有一个想法,创建一个多态的个人资料模型,并将所有的图片附加到那些有“一对一”关系的人身上可能会更好,但即使是这样…我真的很想得到一些帮助,弄清楚为什么我现在无法实现这一点,这样我就可以了解下一步该怎么做。我完全感到困惑。尝试将
事件参数
方法更新为:
def event_params
params.require(:event).permit(:title, :description, :image_url, :company_id, :venue_id,
production_artists_attributes: [ :id, :event_id, :artist_id, :artist_type_id, :role, :_destroy,
venues_attributes: [ :id, :name,
imageable_attributes: [:image]]] )
end
这里的问题是,您将的设置为
选项,将的has_one
关系设置为可成像的
,并且您可能需要在模型和模型上的字段中对视图执行类似的操作:
accepts_nested_attributes_for :imageable
<%= f.fields_for :imageable do |image_upload| %>
<%= image_upload.file_field :image, class: "form-control" %>
<% end %>
接受\u嵌套的\u属性\u:imageable
让我知道它是怎么回事。在我的头撞了几周墙之后……我解决了它……但我觉得自己像个白痴。我把它留给遇到这个问题的其他人
结果是我不太理解嵌套强参数的语法。通过在图片出现之前关闭制作艺术家和场地的嵌套属性,我能够让它正常工作。我还让它为公司模式工作
所以我改变了这个:
def event_params
params.require(:event).permit(:title, :description, :image_url, :company_id, :venue_id,
production_artists_attributes: [ :id, :event_id, :artist_id, :artist_type_id, :role, :_destroy,
venues_attributes: [ :id, :name,
picture_attributes: [:image]]] )
end
为此:
def event_params
params.require(:event).permit(:title, :description, :image_url, :company_id, :venue_id,
production_artists_attributes: [:id, :event_id, :artist_id, :artist_type_id, :role, :_destroy],
venues_attributes: [:id, :name],
picture_attributes: [:image])
end
一切都很顺利。谢谢您的回复。试过这个。现在,我收到一条“Unpermitted parameters:imageable”错误消息,数据库再次没有任何内容。您是否在事件参数
方法中为imageable属性添加了正确的名称?是的,我做了更改。我真是不知所措。我确实重新启动了服务器,但它仍然不能工作。我只是在学习如何使用控制台,所以我对它没有做太多的工作。
accepts_nested_attributes_for :imageable
<%= f.fields_for :imageable do |image_upload| %>
<%= image_upload.file_field :image, class: "form-control" %>
<% end %>
def event_params
params.require(:event).permit(:title, :description, :image_url, :company_id, :venue_id,
production_artists_attributes: [ :id, :event_id, :artist_id, :artist_type_id, :role, :_destroy,
venues_attributes: [ :id, :name,
picture_attributes: [:image]]] )
end
def event_params
params.require(:event).permit(:title, :description, :image_url, :company_id, :venue_id,
production_artists_attributes: [:id, :event_id, :artist_id, :artist_type_id, :role, :_destroy],
venues_attributes: [:id, :name],
picture_attributes: [:image])
end