Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby显示对象_Ruby On Rails - Fatal编程技术网

Ruby on rails Ruby显示对象

Ruby on rails Ruby显示对象,ruby-on-rails,Ruby On Rails,更新 我的表演功能 def show @contact = Contact.find(params[:id]) @data = Register.all :include => {:session =>[:term, :course]} , :conditions => ["contact_id = ?", params[:id]] respond_to do |format| format.html # show.html.erb format.xml { ren

更新

我的表演功能

def show
@contact = Contact.find(params[:id])
@data = Register.all :include => {:session =>[:term, :course]}  , :conditions => ["contact_id = ?", params[:id]]
respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @contact }
end
结束

模型

class Register < ActiveRecord::Base
 belongs_to :session
 belongs_to :contact
end

class Session < ActiveRecord::Base
 belongs_to :term  
 belongs_to :course
 has_many :registers
 has_many :contacts, :through => :registers
end
我想我做错了

    <% @data.Register.each do |c| %>
    <tr>
        <td><%=h c.Term.number %></td>
        <td><%=h c.Course.course_name %></td>
   </tr>
   <% end %>


感谢

Ruby on Rails的惯例是类是大写的,而类的实例不是大写的

如果模型注册包含声明

has_one term
然后你可以使用

registration = Registration.find xxx  # get an instance of the Registration model
registration.term # access the associated term model
                  # do not use registration.Term
所以

1) 你需要给我们看看你的控制器代码。还有你的ActiveRecord类文件——你是否正确定义了使用“所属”的关系,是否有许多声明

2) 您的软件应该更像:

# Controller
def show
   # Shows all the registrations for person_id
   # args: params['person_id']
   @registrations = Register.find_all_by_person_id(params['person_id'].to_i_
end

# View 

<% @registrations.each do |r| %>
<tr>
    <td><%=h r.term.number %></td>
    <td><%=h r.course.course_name %></td>
</tr>
<% end %>    
#控制器
def秀
#显示人员id的所有注册
#args:params['person_id']
@registrations=Register.按个人id(params['person\u id'])查找所有个人信息,并发送至_
结束
#看法
增加


另外,在命名模型“会话”时要非常小心--潜在的问题是,如果启用db支持的会话,CGI会话可能会存储到模型会话中。

感谢您的回复,我添加了我的控制器方法和一些模型代码。是的,关系正常,我认为我的查找错误。查看development.log以查看查找的sql是否符合您的要求。另外应该是:@contact=contact.find(params[:id].to_i)谢谢,我会玩它的。
# Controller
def show
   # Shows all the registrations for person_id
   # args: params['person_id']
   @registrations = Register.find_all_by_person_id(params['person_id'].to_i_
end

# View 

<% @registrations.each do |r| %>
<tr>
    <td><%=h r.term.number %></td>
    <td><%=h r.course.course_name %></td>
</tr>
<% end %>