简体   繁体   English

Rails:将数据从一个控制器中的实例变量传递到另一个控制器中的实例变量

[英]Rails: passing data from instance variable in 1 controller to instance variable in another controller

I have a Rails app which has 2 databases. 我有一个有2个数据库的Rails应用程序。

  1. Legacy DB with table called : Businesses 带有表的旧版数据库:Businesses
  2. regular development DB that comes with the rails app. rails应用程序附带的常规开发数据库。

I have data in the Businesses table that I want to put into the development DB. 我在Businesses表中有要放入开发数据库的数据。 To accomplish this I have taken the following steps: 为此,我采取了以下步骤:

  1. Set up the app so that I can read from the Businesses DB and see the output in the browser. 设置应用程序,以便我可以从Businesses DB中读取并在浏览器中查看输出。 I accomplished this by creating a model Business and a BusinessesController class which reads all the data from the businesses table and stores the entries in an instance variable @businesses 我通过创建模型BusinessBusinessesController类来完成此任务,该类从businesses表中读取所有数据,并将条目存储在实例变量@businesses

  2. Then I created a model called Listing and a ListingsController . 然后,我创建了一个名为ListingListingsController的模型。 I would like to read all the entries from @businesses in the BusinessesController and store them in @listings in the ListingsController . 我想从阅读所有条目@businessesBusinessesController并将其存储在@listingsListingsController

Thus essentially all I need to do is take data stored in one instance variable and save it in another instance variable. 因此,基本上我要做的就是获取存储在一个实例变量中的数据并将其保存在另一个实例变量中。 I'm not sure how to do this in Rails. 我不确定如何在Rails中执行此操作。

So far I have the following classes: 到目前为止,我有以下课程:

Buisiness 提供商家

class Business < ActiveRecord::Base  
  establish_connection "Listings_development"
end

class BusinessesController < ApplicationController
  def get_all
    @businesses = Business.all
  end

  def index
    self.get_all
    respond_to do |format|
      format.html #index.html.erb
    end
  end
end

index.html.erb index.html.erb

<h1>Listing businesses</h1>

<table>
  <tr>
    <th>Index</th>
    <th>Name</th>
    <th>Phone Number</th>
    <th>Suite</th>
    <th>Address</th>
    <th>City</th>
    <th>Province</th>
    <th>Postal Code</th>
    <th>Fax</th>
    <th>Latitude</th>
    <th>Longitude</th>
    <th>Website</th>
  </tr>

  <% count = 0 %>
  <% @businesses.each do |business| %>
  <!--<%=business.inspect %> <br> <br>-->
  <%count = count.to_i + 1 %> 
  <tr>
    <td><%= business.bid %></td>
    <td><%= business.company_name %></td>
    <td><%= business.phone_number %></td>
    <td><%= business.suite_number %></td>
    <td><%= business.address %></td>
    <td><%= business.city %></td>
    <td><%= business.province %></td>
    <td><%= business.postal_code %></td>
    <td><%= business.fax_number %></td>
    <td><%= business.latitude %></td>
    <td><%= business.longitude %></td>
    <td><%= business.website %></td>    
  <% end %>
</table>

<br />
<%= link_to 'New Business', new_business_path %>

Listings 房源

class Listing < ActiveRecord::Base
  attr_accessor :name, :telephone

  def initialize(attributes = {})
    @name = attributes[:name]
    @telephone = attributes[:telephone]
    @latitude = attributes[:latitude]
    @longitude = attributes[:longitude]

    puts 'Created a new Listing'
  end
end

class ListingsController < ApplicationController
  def get_all
    @listings = @businesses
    # @listings = businesses_controller.get_all
  end

  def index   
    self.get_all

    respond_to do |format|
      format.html #index.html.erb
    end
  end
end

index.html.erb index.html.erb

<h1>Listings</h1>

<p>This is where all Listings will show up</p>
<%= @listings.inspect %> <br/>
<%= @businesses.inspect %>

When I go to url 当我去网址

http://localhost:3000/businesses

I can see the table of all the entries in my legacy DB - businesses table in the browser 我可以在浏览器中查看旧数据库-企业表中所有条目的表

But when I go to url: 但是当我去网址:

http://localhost:3000/listings

I just see the place holder text and for values of @businesses & @listings I see nil . 我只看到占位符文本以及@businesses@listings值,我看到的是nil

So clearly the @businesses variable is not accessible within the ListingsController class. 因此,很明显@businesses变量在ListingsController类中不可访问。 I'm wondering how to best pass data between these 2 controllers. 我想知道如何在这两个控制器之间最好地传递数据。

You are doing it wrong! 你做错了! You never should need a controller to talk to another controller in a MVC pattern. 您永远不需要控制器就可以与MVC模式中的另一个控制器进行通信。 Your model should be the only responsible to know how to get the data, and so you would use that model in both controllers. 您的模型应该是知道如何获取数据的唯一负责人,因此您将在两个控制器中都使用该模型。 Business logic lives at models. 业务逻辑存在于模型中。

You should use Business model inside Listings controller 您应该在清单控制器中使用业务模型

@listings = Business.all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM