Simple_form 必填字段不起作用 - Ruby on Rails [英] Simple_form required field does not work - Ruby on Rails

查看:95
本文介绍了Simple_form 必填字段不起作用 - Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RoR 应用程序中有一个提交表单,使用 simple_form 构建.当字段为空时,应用程序仍会进入下一步,而不会提示错误或警告.默认情况下,这些字段应该是 required: true;但即使手动编写也不起作用.

I have a submiting form in a RoR app, built with simple_form. When the fields are blank, the app still goes to the next step without prompting an error or warning. The fields are supposed to be by default required: true; but even writing it manually does not work.

该应用有 3 个步骤:NewPost(新视图)-> 预览(创建视图)-> 发布.

The app has 3 steps: NewPost (new view) -> Preview (create view) -> Post.

摘录我的控制器和视图会更清楚:

It would be more clear with a extract of my controller and views:

def new
    @post= Post.new
end

def create
    @post = Post.new(params.require(:post).permit(:title, :category_id))

    if params[:previewButt] == "Continue to Preview your Post"
      render :create
    elsif params[:createButt] == "OK! Continue to Post it"
      if @post.save!
      redirect_to root_path
      else
      render :new
      end 
    elsif params[:backButt] == "Make changes"
      render :new
    end
  end

我的视图(摘录):

<%= simple_form_for @post do |form| %>
<%= form.input :title, input_html: { class: 'post-title-input' }, label: false, hint: '"Your post title"' %>
<%= form.collection_radio_buttons(:category_id, Category.all, :id, :name, :item_wrapper_class => "selectable" ) %>
<%= form.button :submit , name: "previewButt", value: "Continue to Preview your Post",  class: "btn btn-lg btn-primary btn-preview" %>
<% end %>

我的创建视图(摘录):

<%= simple_form_for @post do |form| %>
<%= form.hidden_field :title, {:value => @post.title} %>
<%= form.hidden_field :category_id, {:value => @post.category_id} %>
<% end %>

注意不是保存时的问题,模型定义工作正常,问题只在simple_form.

Note that the problem is not when saving, the model definitions work ok, the problem is only in the simple_form.

class Post < ActiveRecord::Base
    belongs_to :category
    validates :title, presence: true
    validates :category_id, presence: true
end

解决方案感谢 DickieBoy 提示:

将控制器更改为:

      def create
        @post = Post.new(params.require(:post).permit(:title, :category_id))

        if params[:previewButt] == "Continue to Preview your Post"
           if @post.valid? 
              render :create
           else
              render :new 
        elsif params[:createButt] == "OK! Continue to Post it"
          if @post.save!
          redirect_to root_path
          else
          render :new
          end 
        elsif params[:backButt] == "Make changes"
          render :new
        end
      end

推荐答案

它会进入创建视图,因为你没有告诉它任何不同.

Its going to the create view because you don't tell it any different.

@post = Post.new(params.require(:post).permit(:title, :category_id))

创建一个新的 Post 实例,其中的参数是空的.新调用在验证方面没有任何作用.你想要这样的东西:

Creates a new instance of Post with the parameters given from the form which are empty. The new call does nothing in terms of validation. You want something like:

@post = Post.new(params.require(:post).permit(:title, :category_id))

if @post.valid? && params[:previewButt] == "Continue to Preview your Post"
....

这篇关于Simple_form 必填字段不起作用 - Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆