Ruby on rails RubyonRails-图像上传
我想向模型中添加图像。我让用户使用Desive进行设置,他们有很多正在销售的商品 我现在想向项目模型添加一组图像,但不确定这是否是最佳方法 我看了一个回形针,但只能做一个图像。还研究了载波,但不确定如何在现有模型上实现 这是我的一些代码 项目1.rb 项目\u controller.rb Item/new.html.erb的表单Ruby on rails RubyonRails-图像上传,ruby-on-rails,Ruby On Rails,我想向模型中添加图像。我让用户使用Desive进行设置,他们有很多正在销售的商品 我现在想向项目模型添加一组图像,但不确定这是否是最佳方法 我看了一个回形针,但只能做一个图像。还研究了载波,但不确定如何在现有模型上实现 这是我的一些代码 项目1.rb 项目\u controller.rb Item/new.html.erb的表单 您只需要创建一个图像模型。并将与Item的关系设置为:Item有多个:images,images属于:Item 但是,是的,回形针是个好的开始 编辑:哦,欢迎Rail
您只需要创建一个图像模型。并将与Item的关系设置为:Item有多个:images,images属于:Item 但是,是的,回形针是个好的开始
编辑:哦,欢迎Rails,你会发现一个很好的社区随时准备提供帮助。您可能会发现搜索“接受”的“嵌套属性”也很有用,这样您就可以将图像上载到项目表单中,并可以动态添加和删除项目表单上的图像。谢谢您的回答。那么,我是否正确地假设我将创建与将项目与用户关联几乎相同的关联?我现在就去谷歌,搜索接受和cocoon的嵌套属性。是的,你是对的。一旦您知道我们已回答您的原始问题,请将此问题标记为已解决。如果你有新问题,就根据你的具体问题提出新问题。这正是我需要的。我明天会实施,看看进展如何。对于视图,是否可以使用@item.images.each访问它们,然后进行迭代?如果您想显示上载的图像,则必须使用@item.images.each do | image | end-我可以看看您如何将picture.url方法委托给您的图像,但由于它有很多,这将是相当棘手的
#app/models/item.rb
class Item < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
validates :title, :price, :description, presence: true
end
#app/models/image.rb
class Image < ActiveRecord::Base
belongs_to :item
has_attached_file :picture
end
class ItemsController < ApplicationController
before_action :findItem, only: [:edit, :update, :sold]
def index
@items = Item.all
end
def new
@item = Item.new
end
def create
@item = Item.create(item_params)
@item.user = current_user
if @item.save
flash[:success] = "Your item was successfully listed."
render 'show'
else
flash[:error] = "Your item could not be listed. Please try again."
render 'new'
end
end
def show
@item = Item.find(params[:id])
end
def edit
end
def update
if @item.update(item_params)
flash[:success] = "Your item listing was updated successfully."
redirect_to item_path(@item)
else
flash[:error] = "Your listing was not updated. Please try again."
render 'edit'
end
end
def sold
@item.toggle(:sold)
@item.save
redirect_to item_path(@item)
end
private
def item_params
params.require(:item).permit(:title, :price, :description)
end
def findItem
@item = Item.find(params[:id])
end
end
<div class="row">
<div class="col-xs-12">
<%= form_for(@item, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :title %>
</div>
<div class="col-sm-8">
<%= f.text_field :title, class: "form-control", placeholder: "What are you selling?", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :price %>
</div>
<div class="col-sm-8">
<%= f.number_field :price, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :description %>
</div>
<div class="col-sm-8">
<%= f.text_area :description, rows: 10, class: "form-control", placeholder: "Describe your item. The more detail you include, the more likely it is to sell." %>
</div>
</div>
<div class="form-group">
<div class="center col-sm-offset-1 col-sm-10">
<%= f.submit class: "btn btn-primary btn-lg" %>
</div>
</div>
<% end %>
<div class="center col-xs-4 col-xs-offset-4">
<%= link_to "[ Cancel and return to listing page ]", items_path %>
</div>
#app/models/item.rb
class Item < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
validates :title, :price, :description, presence: true
end
#app/models/image.rb
class Image < ActiveRecord::Base
belongs_to :item
has_attached_file :picture
end
#app/controllers/items_controller.rb
class ItemsController < ApplicationController
def new
@item = Item.new
@item.images.build
end
def create
@item = Item.new item_params
@item.save
end
private
def item_params
params.require(:item).permit(images_attributes: [:picture])
end
end
#app/views/items/new.html.erb
<%= form_for @item do |f| %>
<%= f.fields_for :images do |i| %>
<%= i.file_field :picture %>
<% end %>
<%= f.submit %>
<% end %>