Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails关联不能为空_Ruby On Rails_Ruby_Ruby On Rails 3_Associations - Fatal编程技术网

Ruby on rails Rails关联不能为空

Ruby on rails Rails关联不能为空,ruby-on-rails,ruby,ruby-on-rails-3,associations,Ruby On Rails,Ruby,Ruby On Rails 3,Associations,我正在为一个项目构建RubyonRails中的教程应用程序,并尝试在两个模型之间创建关联 在我的数据库中,有用户、事件和与用户电子邮件和事件代码关联的考勤表 我自己也尝试过研究如何做到这一点,但每次我尝试验证发送给用户的考勤电子邮件时,它都会声明用户不能为空,就像我试图创建一个新的一样 RubyonRails还是一个新手,所以任何建议都将不胜感激!模型如下 用户模型: 出席模式: 到目前为止,我只是试图加强用户和出勤率之间的关联,一旦我开始工作,我将对活动进行同样的操作。另外,这是Rails 3

我正在为一个项目构建RubyonRails中的教程应用程序,并尝试在两个模型之间创建关联

在我的数据库中,有用户、事件和与用户电子邮件和事件代码关联的考勤表

我自己也尝试过研究如何做到这一点,但每次我尝试验证发送给用户的考勤电子邮件时,它都会声明用户不能为空,就像我试图创建一个新的一样

RubyonRails还是一个新手,所以任何建议都将不胜感激!模型如下

用户模型:

出席模式:

到目前为止,我只是试图加强用户和出勤率之间的关联,一旦我开始工作,我将对活动进行同样的操作。另外,这是Rails 3.2.19和Ruby 1.9.3

编辑:这是我用于表单的代码,我相信它是有效的,因为在我将验证放入模型之前,它是在考勤表中创建行

<% provide(:title, 'Test Event') %>
<h1>Attendance Registration</h1>

<div class="row">
    <div class="span6 offset3">
      <%= form_for(@attendance) do |f| %>
        <%= render 'shared/attendance_error_messages' %>

        <%= f.label :email %>
        <%= f.text_field :email %>

        <%= f.label :code %>
        <%= f.text_field :code %>

        <%= f.submit "Submit", class: "btn btn-large btn-primary" %>
      <% end %>
    </div>
</div>
另外,如果有帮助的话,这是考勤员

class AttendancesController < ApplicationController
  def new
    @attendance = Attendance.new
  end

  def create
    @attendance = Attendance.new(params[:attendance])
    if @attendance.save
      flash[:success] = "Attendance logged."
        redirect_to root_path
    else
        render 'new'
    end
  end
end

将此行添加到表单中以避免出现用户状态错误

<%= f.hidden_field :user_id, current_user.id %>

当您遇到此错误时,您能告诉我更多信息吗?我创建了一个页面,允许某人输入电子邮件和代码,以便在指定的考勤表中创建新行。然而,当我这样做的时候,我得到一个闪光错误,说*用户不能为空。此外,我知道此页面用于创建新的考勤行,因为在我在考勤模型中输入验证之前,它是正确创建该行的。在您定义的用户模型中,您可能与的反向\u有关系,请尝试指定考勤模型中的反向\u,也属于:User,:inverse\u of:Attentences不幸的是,它似乎仍然存在相同的问题,说明用户不能为空。粘贴表单的代码添加到该行后,它似乎仍然存在与用户为空相同的问题。@RSB,这种方法有效,但有其漏洞。看看我的回答。
class AttendancesController < ApplicationController
  def new
    @attendance = Attendance.new
  end

  def create
    @attendance = Attendance.new(params[:attendance])
    if @attendance.save
      flash[:success] = "Attendance logged."
        redirect_to root_path
    else
        render 'new'
    end
  end
end
<%= f.hidden_field :user_id, current_user.id %>