Ruby on rails Rails:创建登录/注册后未填充列

Ruby on rails Rails:创建登录/注册后未填充列,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,在创建登录/注册页面之前,我制作了一个运行良好的应用程序。现在,当创建发票时,Salesperson字段保持空白。我将其设置为在不首先创建登录名的情况下无法创建新发票,但即使如此,当以用户身份登录时,Salesperson字段仍然为空 以下是页面代码: <th>Client</th> <th>Salesperson</th> <th>Amount</th> <th>Date</th> <th c

在创建登录/注册页面之前,我制作了一个运行良好的应用程序。现在,当创建发票时,Salesperson字段保持空白。我将其设置为在不首先创建登录名的情况下无法创建新发票,但即使如此,当以用户身份登录时,Salesperson字段仍然为空

以下是页面代码:

<th>Client</th>
<th>Salesperson</th>
<th>Amount</th>
<th>Date</th>
<th colspan="3"></th>


<tbody>
<% @invoices.each do |invoice| %>
<tr>
<td><%= invoice.company %></td>
<td><%= invoice.salesperson %></td>
<td><%= invoice.amount %></td>
<td><%= invoice.date %></td>
<td><%= link_to 'Display', invoice %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
客户端
销售员
数量
日期
在创建登录页面后,如果不填充列,我做错了什么?我有点像在黑暗中拍摄,因为没有对发票控制器或代码进行任何更改。刚刚添加了一个用户模型和new.html.erb

My_form.html.erb部分提取:

Company Salesperson
<address>
<%= f.input_field :salesperson, class: "form-control", id: "5" %>
<input class="form-control" id= "6"  placeholder="address line 1" />
<input class="form-control" id= "7"  placeholder="address line 2" />
<input class="form-control" id= "8"  placeholder="phone number" />
</address>
</div><!-- /.col -->
<div class="col-xs-4 invoice-col">
<form class="form-horizontal">
公司销售人员
这是发票控制员:

class InvoicesController < ApplicationController
before_filter :authorize, only: [:edit, :update, :create]
before_action :set_invoice, only: [:show]

# GET /invoices
# GET /invoices.json
def index
@search = Invoice.search(params[:q])
@invoices = @search.result
@invoices = Invoice.all.order("created_at DESC").paginate(page: params[:page], per_page: 8)
end

def search
index
render :index
end

# GET /invoices/1
# GET /invoices/1.json
def show
end

# GET /invoices/new
def new
@invoice = Invoice.new
end

# POST /invoices
# POST /invoices.json
def create
@invoice = Invoice.new(invoice_params)

respond_to do |format|
  if @invoice.save
    format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
    format.json { render :show, status: :created, location: @invoice }
  else
    format.html { render :new }
    format.json { render json: @invoice.errors, status: :unprocessable_entity }
  end
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
  @invoice = Invoice.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
  params.require(:invoice).permit(:amount, :company, :contragent, :currency, :date)
end
end
class InvoicesController
假设您有一个助手,如
当前用户
(或另一个检索当前登录用户的助手):

invoices\u controller.rb

def create
  @invoice = Invoice.new(invoice_params)
  @invoice.salesperson = current_user
  ... your existing code ...
end
创建操作永远不会将销售人员分配给发票,除非您告诉它这样做

编辑/添加

为了确保未来的发票必须有关联的销售人员,您可以将其验证添加到
invoice.rb
文件中:

class Invoice < ActiveRecord::Base
  belongs_to :salesperson
  validates_prescence_of :salesperson
  ...
end
class发票

这样,如果在没有销售人员登录的情况下提交了新发票的表单,发票将无法保存,并且您可以在表单中显示错误。

InvoicesController上的
invoices#create
操作将为每个发票设置这些字段。首先,我们需要看到这一点。第二,我的假设是,
发票
属于:销售人员
,但如果您不共享更多相关信息,我们不知道
发票
销售人员
之间的关系。添加的发票控制器我有一个会话控制器来检索当前登录的用户。我现在就试试。谢谢这是作为销售人员显示的内容:#我是否需要运行活动记录迁移到associate?rails g migration AddSalesorsonToInvoice salesperson:Attensions\u to是,假设您希望发票属于某个销售人员。这看起来已经解决了,但现在我遇到了这个问题,因此必须在两个数据库之间创建关系,因为它们确实存在:PG::UndefinedTable:ERROR:relation“Salesopers”不存在您是否有专门命名为销售人员的表?或者您是否有一个
用户
表,其中
销售人员
是一种用户类型?