[英]Store data from one table to another in rails
I have a fundings table and organisations table. 我有一个资金表和组织表。 Fundings table has organisation_id.
资金表中有organisation_id。 In fundings table I have fields that are same with organisations table.
在资金表中,我的字段与组织表相同。 What I want is when application is created and on funding show page there is a add button to add all fields that are same in fundings and organisations table gets stored in organisations table as well.
我想要的是在创建应用程序并且在资金显示页面上有一个添加按钮,以添加资金和组织表中相同的所有字段以及组织表也存储在组织表中。 Please help
请帮忙
_form.html.erb (funding_form) _form.html.erb(funding_form)
<%= form_for([@parent, @child, @funding], :html => {class: "form-horizontal",role: "form"}) do |form| %>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :activity_details, class: "required" %>
</div>
<div class="col-sm-8">
<%= form.text_area :activity_details, required: true %>
</div>
</div>
<!--Adding organisation -->
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :name_of_organisation, id: "name_of_organisation-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :name_of_organisation, class: "form-control hidden", id: "name_of_organisation-textfield" %>
</div>
</div>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :address, id: "address-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :address, class: "form-control hidden", id: "address-textfield" %>
</div>
</div>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :city, id: "city-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :city, class: "form-control hidden", id: "city-textfield" %>
</div>
</div>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :province, id: "province-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :province, class: "form-control hidden", id: "province-textfield" %>
</div>
</div>
<!-- End Adding organisation -->
<div class="form-group">
<div class="col-sm-10">
<%= form.submit "Apply", class: 'btn btn-primary btn-lg' %>
</div>
</div>
</div>
</div>
<% end %>
show.html.erb(funding show page) show.html.erb(资金显示页面)
<p>
<strong>Name of Organisation:</strong>
<%= @funding.name_of_organisation %></p><br>
<p>
<strong>Address:</strong>
<%= @funding.address %></p><br>
<p>
<strong>City:</strong>
<%= @funding.city %></p><br>
<p>
<strong>Province:</strong>
<%= @funding.province %></p><br>
<p>
<% if current_user.admin? %>
<%= link_to 'Add Organisation', "" %>
<% end %>
if admin clicks on add organisation fields get added to organisation table. 如果管理员单击“添加组织”字段,则会添加到组织表中。
schema funding table 模式资金表
t.text "activity_details"
t.string "city"
t.string "name_of_organisation"
t.string "province"
t.text "address"
schema organisations table 架构组织表
t.string "name_of_organisation"
t.text "address"
t.string "city"
t.string "province"
funding.rb funding.rb
belongs_to :organisation
organisation.rb organisation.rb
has_many :fundings
organisations controller I have a new and create method to create organisation by the admin directly.so i have added a new_org method to get add the organisation from the funding table. 组织控制器我有一个新的创建方法,可以直接由管理员创建组织。所以我添加了一个new_org方法,以便从资金表中添加组织。 But I am not able to find out how to implement it.
但是我不知道如何实现它。
def new_org
@organisation = Organisation.new
end
def create_org
@organisation = Organisation.new(organisation_params)
if @organisation.save
flash[:success] = "Organisation is added"
redirect_to main_admin_service_provider_path
else
render 'new'
end
end
Something like this should work: 这样的事情应该起作用:
app/controllers/fundings_controller.rb 应用程序/控制器/ fundings_controller.rb
class FundingsController < ApplicationController
before_action :set_funding, on: %i[show add_organisation]
# ...
def update_organisation
return head :forbidden unless current_user.admin?
# assuming funding always has an organisation
whitelist = %w[your field names]
@funding.organisation.update!(@funding.attributes.slice(*whitelist))
redirect_to @funding, notice: 'fields successfully added to organisation'
end
# ...
private
def set_funding
@funding = Funding.find(params[:id])
end
end
config/routes.rb 配置/ routes.rb中
Rails.application.routes.draw do
# ...
resources :fundings, only: :show do
# using patch because we are updating an existing resource
# but you can change this to whatever you like
patch 'update_organisation', on: :member
end
# ...
end
app/views/fundings/show.html.erb 应用程序/视图/拨款,/ show.html.erb
<% if current_user.admin? %>
<%= link_to 'Add Organisation', update_organisation_funding_path(@funding), method: :patch %>
<% end %>
Alternatively you can update all double fields (without whitelisting) using the following code: 或者,您可以使用以下代码更新所有双字段(不添加白名单):
double_attribute_names = @funding.attribute_names & @funding.organisation.attribute_names
@funding.organisation.update!(@funding.attributes.slice(*double_attribute_names)
However keep in mind that this may produce unwanted results. 但是请记住,这可能会产生不良结果。 For example some fields you don't want to update like
'id'
, 'created_at'
, 'updated_at'
are most likely present on both instances, and maybe some custom double fields. 例如,某些您不想更新
'created_at'
,例如'id'
, 'created_at'
和'updated_at'
都很可能同时出现在这两个实例上,也许还有一些自定义的double字段。
This can be resolved by creating a blacklist to exclude those fields: 可以通过创建黑名单来排除这些字段来解决此问题:
blacklist = %w[id created_at updated_at]
double_attribute_names = @funding.attribute_names & @funding.organisation.attribute_names
@funding.organisation.update!(@funding.attributes.slice(*double_attribute_names).except(*blacklist))
But generally speaking it's better to whitelist. 但总的来说,最好将其列入白名单。
This is not a good way to do it but you can define a hidden form with hidden fields with values of @funding.x
on show page by defining @organization = Organization.new
in show method on Fundings.controller
then you can simply post it as normal form. 这不是一个好方法,但是您可以通过在
Fundings.controller
show方法中定义@organization = Organization.new
,在显示页面上使用值为@funding.x
@organization = Organization.new
隐藏字段来定义隐藏表单,然后只需将其发布即可作为正常形式。 Or you can show this form to only admins. 或者,您可以仅向管理员显示此表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.