我访问页面时会自动提交Rails表单

时间:2014-09-28 16:06:38

标签: ruby-on-rails forms validation submit

我有一个评级表,里面有5个收音机和一个提交按钮。问题是当我访问此页面时,由于某种原因它尝试提交表单(当然,“零”值)。验证不允许这样做,因此它呈现错误消息,这不是很好。

评级表:

= simple_form_for @shop.ratings.find_or_create_by(user_id: user_id), 
                                    :html => {:id => form_id, 
                                    :class => "star_rating_form"} do |f|
  = f.hidden_field :shop_id, :value => @shop.id
  - if signed_in?
    = f.hidden_field :user_id, :value => current_user.id
  = f.input :stars, 
      :label => "", 
      :collection => [[1], [2], [3], [4], [5]], 
      :label_method => :last, 
      :value_method => :first,
      :as => :radio_buttons, 
      :item_wrapper_class => 'inline',
      :checked => true
  = f.submit

评级控制器:

class RatingsController < InheritedResources::Base
  belongs_to :shop
  actions :create, :update

  def create
    @shop = Shop.find(params[:rating][:shop_id])
    super
  end

  def update
    @shop = Shop.find(params[:rating][:shop_id])
    super
  end

  private

    def permitted_params
      params.permit(:rating => [:stars])
    end

我尝试像super unless params[:rating][:stars] == 0那样做,但是没有帮助。

PS其余表格效果很好。

1 个答案:

答案 0 :(得分:2)

我不确定但是这个bcoz你正在使用find_or_create_by,它立即创建对象,尝试find_or_initialize_by。或者在javascript的某个地方查看你指定的函数onload(){form.submit(); }已指定。希望它可以提供帮助