我正在使用 Rails 5.2.2 。我安装了 Bullet Gem 并将其打开bullet.log:
2019-01-18 13:22:02[WARN] user: jordan
GET /customers/37
USE eager loading detected
Account => [:user]
Add to your finder: :includes => [:user]
Call stack
/home/jordan/Desktop/Rails/acc/app/views/customers/show.html.erb:64:in `block in _app_views_customers_show_html_erb___50963452045031815_70292526903360'
/home/jordan/Desktop/Rails/acc/app/views/customers/show.html.erb:62:in `_app_views_customers_show_html_erb___50963452045031815_70292526903360'
Run Code Online (Sandbox Code Playgroud)
根据我添加includes(:user)到我的account.rb文件中的指南。我尝试将其添加到索引操作并显示操作,但此警报一次又一次出现。
帐户.rb:
class Account < ApplicationRecord
belongs_to :user
belongs_to :customer
end
Run Code Online (Sandbox Code Playgroud)
账户控制器.rb:
def index
@accounts = Account.includes(:user).where(customer_id:params[:id])
end
private
def set_account
@account = Account.includes(:user).find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
show.html.erb(客户 62..64 行)
<% @customer.accounts.each do |ac| %>
<div class="sl-item">
<div class="sl-left"> <img src="<%= …Run Code Online (Sandbox Code Playgroud) 我的数据库上有一个customers表,并且有一个名为“ last_updated_by”的列。我想在此字段中将当前用户名添加为纯文本。
我在我的应用程序上安装了devise,因此它为我提供了current_user参数。
1-
def customer_params
params.require(:customer).permit(:name, :last_updated_by=>current_user.name)
end
2-
def customer_params
params[:last_updated_by] = current_user.name
params.require(:customer).permit(:name, :last_updated_by)
end
3-
def customer_params
last_updated_by = current_user.name
params.require(:customer).permit(:name, :last_updated_by=>current_user.name)
end
Run Code Online (Sandbox Code Playgroud)
如何在控制器中设置一些默认值。
我正在使用 Rails 5.2.2 我的数据库中有许多空(nil)字段,并创建了一个自定义方法以distance_of_time_in_words在我的模型中使用而不会出现错误。
def my_distance_of_time_in_words
if self.accounts.blank?
"No Record Avaliable"
else
distance_of_time_in_words(self.accounts.first.updated_at,Time.now).titleize
end
end
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法从视图中传递我的对象:
<%= @customer.my_distance_of_time_in_words %>
Run Code Online (Sandbox Code Playgroud)
它运行良好,我重新启动了我的电脑,它说:
undefined method `distance_of_time_in_words' for #<Customer:0x00007f43b98601d8>
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为正如我所说,它按我的预期工作。但现在不起作用。