【问题标题】:Failed to display data from the database in Ruby on Rails 5无法在 Ruby on Rails 5 中显示数据库中的数据
【发布时间】:2018-05-24 10:40:57
【问题描述】:

我在数据库中有一些表,它们是 ticketsticket_web_users

ticket_web_users 表包含 idemailpassword 字段。

虽然

tickets 具有 idticket_noweb_user_id 字段。

ticket_web_usersidweb_user_id & ticket_id

每张工单都有很多工单网络用户。在我的show.html.erb上,我想显示包含某个ticketweb 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', it belongs_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


【解决方案1】:

您是否忘记在ticket_web_users 上添加关联

belongs_to :ticket

还有一件事你忘了在ticket_web_users 表上添加ticket_id 吗?

能否提供ticket_web_users 表中ticket 和ticket_web_user 的model 和column 的代码?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多