目前,我可以提交和创建我的表单。但是,问题是,我的表中有一个字段具有nill值,即created_by列。我不知道它的值传递到哪里,因为它不会抛出任何错误。我猜想,我的创建方法无法将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中了。但是,当我调用<%= @commodity_group.user.name %>时,会得到以下错误:
用于nil的未定义方法“`name”: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
endcommodity_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
endcommodity_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发布于 2016-06-25 05:44:45
首先,检查用户id是否正确保存了commodity_object。
如果不是,
请上传从表单中发送的表单和参数。
如果是,,
在commodity_group.rb文件中,您用相同的代码给出了两个belongs_to关系,这是错误的,因此它在created_by and updated_by需要什么关系时感到困惑。
按以下方式更改文件,
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仔细观察两个belongs_to名称。现在可以调用,
<%= @commodity_group.created_user.name %>
<%= @commodity_group.updated_user.name %> created_user和updated_user将根据这些数据进行分析。
https://stackoverflow.com/questions/38025213
复制相似问题