目前我可以提交并创建我的表单。但是,问题是我的表中有一个字段有一个nill值,即created_by列。我不知道它的价值传递到哪里,因为它不会给我任何错误。我想,不知怎的,我的create方法无法将current_user.id的值传递给商品组表中的created_by字段,我想我已经正确设置了用户和商品组之间的关系。 基本上,我想要的只是如果用户创建商品组,他的ID将被保存到表商品组中的created_by列中,如果他更新商品组的信息,他的ID将被保存到列update_by中。我可以通过这样的方式检索他的名字或电子邮件进入视图:
<p>
Created by: <%= @commodity_group.user.name %>
Updated by: <%= @commodity_group.user.name %>
</p>
更新:我不知道current_user.id可以保存到created_by列中。但是,当我调用&lt;%= @ commodity_group.user.name%&gt;时,我收到此错误:
未定义的方法`name&#39;为零:NilClass
以下是所有文件:
user.rb:
class User < ActiveRecord::Base
extend FriendlyId
include Filterable
friendly_id :slug_candidates, use: :history
has_secure_password
acts_as_paranoid
has_many :activities
has_many :pricing_histories
has_many :commodity_groups_created, class_name: 'CommodityGroup',
foreign_key: :created_by
has_many :commodity_groups_updated, class_name: 'CommodityGroup',
foreign_key: :updated_by
end
commodity_group.rb:
class CommodityGroup < ActiveRecord::Base
extend FriendlyId
friendly_id :code, use: :history
belongs_to :created_user,
class_name: 'User',
primary_key: :id,
foreign_key: :created_by
belongs_to :updated_user,
class_name: 'User',
primary_key: :id,
foreign_key: :updated_by
validates_presence_of :code
validates_presence_of :name
# validates_presence_of :user
end
commodity_groups / _form.html.haml :
%div.page-content-wrapper
%div.page-content
= render 'header'
%div.row
.col-md-12
.portlet.light.form-fit
.portlet-title
- if defined? title
.caption
%i.icon-user.font-blue-hoki
%span.caption-subject.font-blue-hoki.bold.uppercase
= title
.portlet-body.form
= form_for @commodity_group, html: {class: 'form-horizontal form-bordered form-label-stripped'} do |f|
.form-body
.form-group
%label.control-label.col-md-3 Code
.col-md-9
= f.text_field :code, placeholder: 'Code', class: 'form-control'
.form-group.last
%label.control-label.col-md-3 Name
.col-md-9
= f.text_field :name, placeholder: 'Name', class: 'form-control'
.form-actions
.row
.col-md-offset-3.col-md-9
%button.btn.green{:type => 'submit'}
%i.fa.fa-check
Submit
= link_to "Cancel", locations_path, class: 'btn default'
commodity_groups_controller.rb:
class CommodityGroupsController < ApplicationController
before_action :set_commodity_group, only: [:show, :edit, :update, :destroy]
# GET /commodity_groups
def index
@commodity_groups = CommodityGroup.all
end
# GET /commodity_groups/1
def show
end
# GET /commodity_groups/new
def new
@commodity_group = current_user.commodity_groups_created.build
end
# GET /commodity_groups/1/edit
def edit
end
# POST /commodity_groups
def create
@commodity_group = current_user.commodity_groups_created.create(commodity_group_params)
if @commodity_group.save!
redirect_to commodity_groups_path, notice: init_message(:success, t('message.new_success', page_name: t('page_name.commodity_group')))
else
redirect_to new_commodity_group_path, notice: init_message(:error, t('message.new_error', page_name: t('page_name.commodity_group')))
end
end
# PATCH/PUT /commodity_groups/1
def update
if @commodity_group.update(commodity_group_params)
@commodity_group.update_columns(created_by: current_user.id)
redirect_to @commodity_group, notice: 'Commodity group was successfully updated.'
else
render :edit
end
end
# DELETE /commodity_groups/1
def destroy
@commodity_group.destroy
redirect_to commodity_groups_url, notice: 'Commodity group was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_commodity_group
@commodity_group = CommodityGroup.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def commodity_group_params
params.require(:commodity_group).permit(:code, :name, :created_by, :updated_by, :slug)
end
end
答案 0 :(得分:0)
首先检查商品对象是否使用用户ID正确保存。
如果不是,
请上传表单中发送的表单和参数。
如果是,
在 commodity_group.rb 文件中,您使用相同的代码给出了两个belongs_to
关系是错误的,因此它对{{1}所需的关系感到困惑}。
按如下方式更改文件,
created_by and updated_by
仔细观察两个belongs_to名称。 现在你可以打电话了,
class CommodityGroup < ActiveRecord::Base
extend FriendlyId
friendly_id :code, use: :history
belongs_to :created_user,
class_name: 'User',
primary_key: :id,
foreign_key: :created_by
belongs_to :updated_user,
class_name: 'User',
primary_key: :id,
foreign_key: :updated_by
validates_presence_of :code
validates_presence_of :name
# validates_presence_of :user
end
created_user和updated_user将根据assosciations进行。