开发者

Basic question on Ruby Rails

开发者 https://www.devze.com 2023-03-01 20:54 出处:网络
Suppose I have model xyz.rb and model abc.rb. No relation between them. If want to print any a开发者_StackOverflowttribute from xyz in abc views/print/show.html.erb how??

Suppose I have model xyz.rb and model abc.rb. No relation between them. If want to print any a开发者_StackOverflowttribute from xyz in abc views/print/show.html.erb how??

I know very basic but looking for good explanation.


You can access any model from any controller, view or helper method. Mvc means model, controllers and views are related but there's no limitation on access between them. The normal thing to do would be to store the reports to an instance variable in any controller then output them in the view:

#print_controller.rb

def show
  @reports = Report.find_by_some_attribute(...

#show.html.erb

<%- @reports.each do |report| -%>
  <%= report.created_at -%>
<%- end -%>

I really think though that you need to find a better approach to learning rails. This is very basic like you say and I would recommend you buy a book. Do you speak English well, or what's your native language?


Something like:

XYZ.all.each do |xyz|
  some(xyz)
end

Details you can find here.


Sure you can.

Assuming @x is an instance of Xyz model, you can do print any attribute of @x object.

If you dont have @x object. You can find it and instantiate @x in the show action of the abc controller. @x = Xyz.first for example.


Considering your example, if you have two models User and Report. You can access Report's created_at from User's controller,view,etc. You need to write something like this:

Report.find_by_created_at("2013-11-12 14:43:44.11824")

You can refer ruby on rails guides to learn rails. Also you can find basic active record explanations here


This very basic but i am giving best solution to this

in abc controller

def show
  @reports = Report.where(:attribute => value)
end

it will get the all records basic on that value

<h1>views/abc/show.html.erb</h1>
<% @reports.each do |report| %>
  <%= report.attribute_name %>
 <%end%>
0

精彩评论

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