我不确定这是否可能使用Carrierwave,我仍然在学习Ruby和Rails,所以请原谅我。
我有一个简单的表格来记录观察结果。每个观察都有可以针对它记录的数据,我将这些数据存储在观察中的JSONB列中(我最终将针对一个观察存储多组数据)。我希望能够通过Carrierwave上传图像,并将其与JSONB列中的数据一起存储,而不是将其保存到自己的列中。因此,我保存的此列数据将是这样的,例如...
{
"ph":"",
"ecoli":"",
"nitri":"Less than 0.5",
"oxygen":"",
"clarity":"bottom visible",
"nitrates":"Less than 0.5",
"conditions":[
"Wildlife Death",
"Shoreline Alterations",
"Water Quality"
],
"observed_on":"2015-06-21T04:45",
"invasive_species":[
"Phragmites",
"Loosestrife",
"Dog-Strangling Vine"
],
"phosphates_threshold":"less than 0.2",
"image":[
"url": "the/url",
"thumbnail: "the/thumbnail/url",
"medium":"the/thumbnail/url"
.....
]
}
部分配套代码供参考…
- 观察.rb*
class Observation < ActiveRecord::Base
belongs_to :user
has_many :comments
serialize :observation_data, HashSerializer
store_accessor :observation_data
end
- observation_data.rb -无表模型 *
class ObservationData < ActiveRecord::Base
belongs_to :observation
mount_uploader :image, ImageUploader
end
- _form.html.erb*
<%= form_for @observation do |f| -%>
...Other form items
<fieldset>
<legend>Observation data</legend>
<%= render "observation_data", :f => f %>
</fieldset>
<div class="actions">
<%= f.submit "Submit", class: "button" %>
</div>
<% end %>
- _observation_data.html.erb*
<%= f.fields_for :observation_data, OpenStruct.new(@observation.observation_data) do |o| %>
...Other form items
<div class="field">
<%= o.label :image, "Upload an image" %>
<%= o.file_field :image %>
</div>
...Other form items
<% end %>
- observations_controller.rb*
class ObservationsController < ApplicationController
before_action :set_observation, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
@observations = Observation.all
@user = current_user.id
end
def show
@observations = Observation.find_by(id: params[:id])
@observation_data = @observation.observation_data
@comments = @observation.comments.all
@comment = @observation.comments.build
@user = User.find_by(id: @observation.user_id)
end
def new
@observation = Observation.new
end
def edit
end
def create
@observation = Observation.new(observation_params)
@observation.user_id = current_user.id
respond_to do |format|
if @observation.save
format.html { redirect_to @observation, notice: 'Observation was successfully created.' }
format.json { render action: 'show', status: :created, location: @idea }
else
format.html { render action: 'new' }
format.json { render json: @observation.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @observation.update(observation_params)
format.html { redirect_to @observation, notice: 'Observation was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @observation.errors, status: :unprocessable_entity }
end
end
end
def destroy
@observation.destroy
respond_to do |format|
format.html { redirect_to root_url }
format.json { head :no_content }
end
end
private
def set_observation
@observation = Observation.find(params[:id])
end
def observation_params
params.require(:observation).permit(:user_id, :lat, :lng, :name, :description, :comment, observation_data: [:observed_on, :image, :ph, :oxygen, :ecoli, :phosphates_threshold, :clarity, :nitri, :nitrates, wildlife: [], conditions: [], invasive_species: []])
end
end
对我来说,问题是mount_uploader :image, ImageUploader
总是在寻找:image
列,我如何将载波响应this与另一列中的JSON合并?这可能吗
1条答案
按热度按时间tzdcorbm1#
是的,你可以做到这一点,所以这就是我如何为
store
选项创建一个 Package 器,并根据他们的建议进行了一些巧妙的覆盖。在您的情况下,可以简化为JSON对象内的主图像键,并与其他数据作为补充。因为这一个的目的是更开放和所有的图像。这是一个例子:
迁移
型号