ActiveResource allows you to easily communicate between multiple Rails applications. See how in this episode.
# models/product.rb
class Product < ActiveResource::Base
self.site = "http://localhost:3000"
end
# models/post.rb
class Post < ActiveRecord::Base
def product
@product ||= Product.find(product_id) unless product_id.blank?
end
end
<!-- views/posts/edit.html.erb -->
<p>
<%= f.label :product_id %>
<%= f.collection_select :product_id, Product.find(:all), :id, :name %>
</p>
<!-- views/posts/show.html.erb -->
<% if @post.product %>
<strong><%=h @post.product.name %></strong>
<% end %>
Rails应用间通信
本篇介绍如何使用ActiveResource实现Rails应用间的轻松通信。通过具体代码示例展示了如何在一个应用中调用另一个应用的资源,包括从产品模型获取数据并在帖子模型中使用。
94

被折叠的 条评论
为什么被折叠?



