开发者

Rails 3: How to save links in a model variable

I'm creating a newsf开发者_如何学运维eed and It'd be helpful if I could save links in the model variable content (for example).

Can that be done?

Thanks

Edit: what I want is to store in the variable :content something like: Donald(link to donald profile) has bought a box(link to box page) from user_x(link to user_x store).


So you basically want to create a link from relations. You should NOT store the raw link in your database, but rather build it dynamically (imagine if an id changed or a product id changed). If I understand your example correctly, a purchase is a self-referencing join between users? See here for implementation setup

class User < ActiveRecord::Base
  def profile_link
    link_to "#{self.first_name} #{self.last_name}", user_profile_path(self)
  end

  def store_link
    link_to "#{self.first_name} #{self.last_name}", user_store_path(self)
  end
end

class Purchase < ActiveRecord::Base
  def product_link
    link_to self.product_name, product_path(self)
  end

  # buyer and seller are the aliases for the self referencing user class
  def info_link
    "#{self.buyer.profile_link} has bought a #{self.product_link} from #{seller.store_link}"
  end
end

where url and title are fields in your table (that you created and populated)

Then in your view:

stuff... <%= @product.info_link %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜