【发布时间】:2018-05-24 10:40:57
【问题描述】:
我在数据库中有一些表,它们是 tickets 和 ticket_web_users。
ticket_web_users 表包含 id、email 和 password 字段。
虽然
表 tickets 具有 id、ticket_no 和 web_user_id 字段。
和
表 ticket_web_users:id、web_user_id & ticket_id
每张工单都有很多工单网络用户。在我的show.html.erb上,我想显示包含某个ticket的web users的ticket信息。简而言之,我想显示该工单的所有网络用户。这是我的控制器:tickets_controller.rb
module TicketManagement
class TicketsController < ApplicationController
before_action :get_ticket_web_user, only: [:show]
def show
end
def get_ticket_web_user
@web_ticket_users = TicketWebUser.where('ticket_id=?',params[:id])
end
end
end
型号:
ticket.rb:
class Ticket < ApplicationRecord
has_many :ticket_histories
belongs_to :user
accepts_nested_attributes_for :ticket_histories
end
ticket_web_user.rb:
class TicketWebUser < ApplicationRecord
belongs_to :web_user
end
这是我的视图 show.html.erb:
<% @web_ticket_users.each do |web_ticket_user| %>
<%= text_field_tag 'web_user',value: web_ticket_user.email , class: 'form-control'%>
<% end %>
显示错误:TicketManagement::Tickets#show 中的 NoMethodError, TicketWebUser:0xb045bab8> 的未定义方法“电子邮件” 我搜索了一些可以帮助我解决问题的文档,包括Ruby on Rails Guides on Active Record Query Interface,但我找不到与我的问题相同的方法。
【问题讨论】:
-
尝试重启你的服务器。如果票有一个字段
web_user_id它不能has_many :web_users', itbelongs_to :web_user. You need to setup your models and associations properly and you can use in show action@ticket = Ticket.find(params[:id])` 并在视图@ticket.web_users
标签: ruby-on-rails