在rails上工作,我有文本字段和文本区域来接收文本,但它显示验证错误"主题不能为空"

时间:2017-06-07 06:31:32

标签: ruby-on-rails ruby validation

在相应的文本字段和区域中键入主题和详细信息后,它仍显示"验证错误" "主题不能为空"。问题是什么?我似乎无法弄明白。

This is the task partial
_task.html.erb:

 <li id="task-<%= task.id %>">
      <%= link_to gravatar_for(task.user, size: 50), task.user %>
      <span class="task"><%= link_to task.user.name, task.user %></span>
      <span class="topiclabel">Title:</span>
      <span class="topic"style=color:black> <%= task.topic %></span>
      <span class="detailslabel">Details: </span>
      <span class="detail"><%= task.detail %></span> 
      <span class="timestamp">
        Posted <%= time_ago_in_words(task.created_at) %> ago.
      </span>
</li> 

Schema:

ActiveRecord::Schema.define(version: 20170526194708) do

  create_table "stages", force: :cascade do |t|
    t.text     "topic"
    t.datetime "duration"
    t.text     "detail"
    t.integer  "task_id"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["task_id"], name: "index_stages_on_task_id"
    t.index ["user_id", "created_at"], name: "index_stages_on_user_id_and_created_at"
    t.index ["user_id"], name: "index_stages_on_user_id"
  end

  create_table "tasks", force: :cascade do |t|
    t.text     "topic"
    t.integer  "member"
    t.text     "detail"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id", "created_at"], name: "index_tasks_on_user_id_and_created_at"
    t.index ["user_id"], name: "index_tasks_on_user_id"
  end

routes.rb:

 resources :tasks,          only: [:create, :destroy]

task.rb
class Task < ApplicationRecord
  belongs_to :user
  has_many :stages
  default_scope -> { order(created_at: :desc) }
   validates :user_id, presence: true
   validates :topic, presence: true, length: { maximum: 140 }
   validates :detail, presence: true, length: { maximum: 140 }

end

Project_pages_controller.rb:

def home
    if logged_in?
      @task = current_user.tasks.build
end


_task_form.html.erb:

<%= form_for(@task) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :topic, placeholder: "Compose new task..." %>
    <%= f.text_area :detail, placeholder: "Write task details..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>


This is the show page

show.html.erb

  <div class="col-md-8">
    <% if @user.tasks.any? %>
      <% if @user.stages.any? %>
      <h3>Tasks (<%= @user.tasks.count %>)</h3>
      <ol class="tasks">
        <%= render @tasks %>
        <%= render @stages %>
      </ol>
      <%= will_paginate @tasks %>
       <% end %>
    <% end %>
  </div>

这是我的控制者;

class TasksController < ApplicationController
    include TasksHelper
  before_action :logged_in_user, only: [:create, :destroy]

  def create
    @task = current_user.tasks.build(task_params)
    if @task.save
      flash[:success] = "Task created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'workshareapp_pages/home'
    end
  end

  def destroy
  end

  private

    def task_params
      params.require(:task).permit(:detail)
    end

end

3 个答案:

答案 0 :(得分:0)

验证:topic,presence:true,length:{maximum:140} 此子句验证记录中的主题属性是否存在且在140个字符长度内。这用于在保存之前验证记录。如果你真的不需要它,你可以删除它。另一个选项是您可以创建专门用于此表单的表单模型。

答案 1 :(得分:0)

它可能是您的模型任务中的验证。

答案 2 :(得分:0)

您遗失了许可证中的:topic字段,这就是为什么它没有被处理。

def task_params
  params.require(:task).permit(:detail, :topic)
end