Ruby on rails 通过其他视图上载图像
我用的是回形针宝石Ruby on rails 通过其他视图上载图像,ruby-on-rails,Ruby On Rails,我用的是回形针宝石 class Business < ActiveRecord::Base has_many :images, :as => :imageable has_many :reviews end class Image < ActiveRecord::Base belongs_to :imageable, :polymorphic => true has_attached_file :photo end class Review <
class Business < ActiveRecord::Base
has_many :images, :as => :imageable
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :photo
end
class Review < ActiveRecord::Base
belongs_to :business
has_many :images, :as => :imageable
end
图像控制器:
def create
@image = @business.images.new(image_params)
if @image.save
flash[:success] = "Image uploaded!"
redirect_to root_url
else
render 'new'
end
end
def create
@review = Review.create(review_params) #This will create photo as well
redirect_to xyz_path
end
private
def review_params
params.require(:review).permit(:content, images_attributes: [:id, :photo, :_destroy])
end
我想能够上传图像也与评论。我是这样想的:
<%= form_for @review do |f| %>
<%= f.text_area :content %>
<%= f.fields_for :photos_attributes do |photo| %>
<%= photo.file_field :image, :multiple => false %>
<% end %>
<%= f.submit 'Submit Review with Image'%>
<% end %>
class Business < ActiveRecord::Base
has_many :images
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :business
belongs_to :review
has_attached_file :photo
end
class Review < ActiveRecord::Base
has_one :image
belongs_to :business
end
既然图像来自图像模型,而其余的则来自审查模型,那么我的审查控制器会是什么样子呢?我的布局方式是这样的- 模型 我稍微修改了表单,使:images的accepts\u嵌套属性\u工作得更好 最后,审查控制员 审查控制员 这将允许您创建一个新的评论,并通过图像向其中添加一张照片。无需转到Image Controller,因为Review可以访问它并接受其属性 我建议在创建/编辑业务时实施类似于业务模型的东西。我将使用业务控制器执行这些操作,并使用accepts\u嵌套的\u attributes\u for:images来允许它接受其属性
希望这有助于回答您的问题。如果我没有正确解释,请告诉我。我会这样安排- 模型 我稍微修改了表单,使:images的accepts\u嵌套属性\u工作得更好 最后,审查控制员 审查控制员 这将允许您创建一个新的评论,并通过图像向其中添加一张照片。无需转到Image Controller,因为Review可以访问它并接受其属性 我建议在创建/编辑业务时实施类似于业务模型的东西。我将使用业务控制器执行这些操作,并使用accepts\u嵌套的\u attributes\u for:images来允许它接受其属性
希望这有助于回答您的问题。如果我没有正确解释,请告诉我。根据您的设计,您将无法知道哪些图像属于哪个评论,而所有图像将属于该评论的母公司业务 一个好的设计是对图像模型使用多态关联
class Business < ActiveRecord::Base
has_many :images, as: :parent
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :parent, polymorphic: true
has_attached_file :photo
end
class Review < ActiveRecord::Base
has_many :images, as: :parent
belongs_to :business
end
或
2-在模型中使用accept_nested_属性,并使用业务_控制器中已有的新建和编辑操作,无需任何代码,图像将与业务一起保存,无论是新的还是仅编辑以添加新图像,并且在视图中使用字段_
如果您不熟悉此链接,请参阅此链接,也可以查看此链接以构建表单
如果您遵循了建议的设计,同样的情况也会出现在审查中。
对于与设计中的审阅模型没有关系的模型::
您可以将操作添加到控制器添加图像
def add_image
@image = @review.business.images.new(image_params)
if @image.save
flash[:success] = "Image uploaded!"
redirect_to root_url
else
render 'YOUR FORM'
end
end
<%= form_for @image, url: path_to_the_action_add_image do |f| %>
<%= f.file_field :photo %>
<%= f.text_area :content %>
<%= f.submit 'Submit Review'%>
<% end %>
这不是一个好的设计,如果您有兴趣知道哪个图像属于哪个评论,那么在该评论中显示哪个图像通过您的设计,您将无法知道哪个图像属于哪个评论,而所有图像都将属于该评论的母公司业务 一个好的设计是对图像模型使用多态关联
class Business < ActiveRecord::Base
has_many :images, as: :parent
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :parent, polymorphic: true
has_attached_file :photo
end
class Review < ActiveRecord::Base
has_many :images, as: :parent
belongs_to :business
end
或
2-在模型中使用accept_nested_属性,并使用业务_控制器中已有的新建和编辑操作,无需任何代码,图像将与业务一起保存,无论是新的还是仅编辑以添加新图像,并且在视图中使用字段_
如果您不熟悉此链接,请参阅此链接,也可以查看此链接以构建表单
如果您遵循了建议的设计,同样的情况也会出现在审查中。
对于与设计中的审阅模型没有关系的模型::
您可以将操作添加到控制器添加图像
def add_image
@image = @review.business.images.new(image_params)
if @image.save
flash[:success] = "Image uploaded!"
redirect_to root_url
else
render 'YOUR FORM'
end
end
<%= form_for @image, url: path_to_the_action_add_image do |f| %>
<%= f.file_field :photo %>
<%= f.text_area :content %>
<%= f.submit 'Submit Review'%>
<% end %>
如果您有兴趣知道哪个图像属于哪个评论,要在该评论中显示哪个评论,那么这不是一个好的设计。你应该在他们之间有一些联系。我加了一个关系。非常适合你的情况。你也可以加很多。您的模型应如下所示:
<%= form_for @review do |f| %>
<%= f.text_area :content %>
<%= f.fields_for :photos_attributes do |photo| %>
<%= photo.file_field :image, :multiple => false %>
<% end %>
<%= f.submit 'Submit Review with Image'%>
<% end %>
class Business < ActiveRecord::Base
has_many :images
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :business
belongs_to :review
has_attached_file :photo
end
class Review < ActiveRecord::Base
has_one :image
belongs_to :business
end
您还需要添加数据库更改。因此,做出相应的改变。在照片模型中添加评论id
我用了更简单的方法。您还可以对图像和其他模型使用多态关联。如果您想添加带有审阅的图像。你应该在他们之间有一些联系。我加了一个关系。非常适合你的情况。你也可以加很多。您的模型应如下所示:
<%= form_for @review do |f| %>
<%= f.text_area :content %>
<%= f.fields_for :photos_attributes do |photo| %>
<%= photo.file_field :image, :multiple => false %>
<% end %>
<%= f.submit 'Submit Review with Image'%>
<% end %>
class Business < ActiveRecord::Base
has_many :images
has_many :reviews
end
class Image < ActiveRecord::Base
belongs_to :business
belongs_to :review
has_attached_file :photo
end
class Review < ActiveRecord::Base
has_one :image
belongs_to :business
end
您还需要添加数据库更改。因此,做出相应的改变。在照片模型中添加评论id
我用了更简单的方法。您也可以对图像和其他模型使用多态关联。您尝试过嵌套属性吗:我假设您的review类有一个:图像或类似的东西?图像和审阅模型之间的关系是什么?@doz87我已经更新了代码。企业主已经可以上传他们企业的图片了。这项新功能允许用户提交带有企业图片的评论
你尝试过嵌套属性:我假设你的评论类有一个:image或类似的东西?image和评论模型之间的关系是什么?@doz87我已经更新了代码。企业主已经可以上传他们企业的图片了。这项新功能允许用户提交带有企业图片的评论。谢谢Nitin。这似乎不起作用。我不明白行:tasks\u attributes:[:id,:photo,:\u destroy]我实际上是在什么时候保存图像的?很抱歉,我拼错了。应该是我更新了我的答案。谢谢尼廷。这似乎不起作用。我不明白行:tasks\u attributes:[:id,:photo,:\u destroy]我实际上是在什么时候保存图像的?很抱歉,我拼错了。应该是我更新了我的答案。