Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 尝试创建事件和非空字段时声明空字段时出错_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails 尝试创建事件和非空字段时声明空字段时出错

Ruby on rails 尝试创建事件和非空字段时声明空字段时出错,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,我收到3个错误,禁止保存此事件:标题不能为空日期不能为空描述不能为空。它工作得很好,但不确定现在出了什么问题 当它们不是空白的时候 这是控制器 class EventsController < ApplicationController before_action :authenticate_user!, :only => [:create , :destroy] before_action :set_event, only: [:show, :edit, :update,

我收到3个错误,禁止保存此事件:标题不能为空日期不能为空描述不能为空。它工作得很好,但不确定现在出了什么问题

当它们不是空白的时候

这是控制器

class EventsController < ApplicationController
  before_action :authenticate_user!, :only => [:create , :destroy]
  before_action :set_event, only: [:show, :edit, :update, :destroy]

  # GET /events
  # GET /events.json
  def index
    @events = Event.all
  end

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

  # GET /events/new
  def new
    @event = Event.new
  end



 # GET /events/1/edit
  def edit
  end

  # POST /events
  # POST /events.json
  def create
    @event = Event.new(event_params)

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

  # PATCH/PUT /events/1
  # PATCH/PUT /events/1.json
  def update
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event, notice: 'Event was successfully updated.' }
        format.json { render :show, status: :ok, location: @event }
      else
        format.html { render :edit }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.json
  def destroy
    @event.destroy
    respond_to do |format|
      format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def event_params
      params.require(:event).permit(:title, :date, :description)
    end
end
表格如下:

<%= form_for(@event) do |f| %>
  <% if @event.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:</h2>

      <ul>
      <% @event.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :date %><br>
    <%= f.date_select :date %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
型号代码符合要求:

class Event < ActiveRecord::Base
  validates_presence_of :title , :date,  :description
  validates_uniqueness_of :title 
end

问题是我安装的一个gem,现在我已经删除了。现在开始工作。

您是否检查了参数散列…参数是否正确…您是否也可以发布模型代码…特别是验证部分。您可以发布从终端发送到服务器的参数吗?我现在发布了Surya建议的模型代码,您可以为请求粘贴te日志输出吗?路由可能也很有用