将数据从一个表存储到另一个表中

时间:2018-07-19 15:31:58

标签: ruby-on-rails ruby ruby-on-rails-5

我有一个资金表和组织表。资金表中有organisation_id。在资金表中,我的字段与组织表相同。我想要的是在创建应用程序并且在资金显示页面上有一个添加按钮,以添加资金和组织表中相同的所有字段以及组织表也存储在组织表中。请帮助

_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(资金显示页面)

<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 %>

如果管理员点击添加组织字段,则添加到组织表中。

模式资助表

t.text "activity_details"
t.string "city"
t.string "name_of_organisation"
t.string "province"
t.text "address"

模式组织表

t.string "name_of_organisation"
t.text "address"
t.string "city"
t.string "province"

funding.rb

belongs_to :organisation

organisation.rb

has_many :fundings

组织负责人 我有一个新的创建方法,可以直接由管理员创建组织。因此,我添加了一个new_org方法来从资金表中添加组织。但是我不知道如何实现它。

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

2 个答案:

答案 0 :(得分:1)

类似的事情应该起作用:

app / controllers / 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

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

<% if current_user.admin? %>
   <%= link_to 'Add Organisation', update_organisation_funding_path(@funding), method: :patch %>
<% end %>

或者,您也可以使用以下代码更新所有重复字段(不列入白名单):

double_attribute_names = @funding.attribute_names & @funding.organisation.attribute_names
@funding.organisation.update!(@funding.attributes.slice(*double_attribute_names)

但是请记住,这可能会产生不需要的结果。例如,您不想更新的某些字段,例如'id''created_at''updated_at'都可能同时出现在这两个实例上,并且可能还有一些自定义的重复字段。

这可以通过创建黑名单来排除这些字段来解决:

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))

但是一般来说,将其列入白名单更好。

答案 1 :(得分:0)

这不是一个好方法,但是您可以通过在显示页面上通过定义值为@funding.x的隐藏字段来定义隐藏表单 @organization = Organization.new中的Fundings.controller的show方法中,您只需将其发布为普通形式即可。或者,您可以只向管理员显示此表单。