我有一個包含5個收音機和一個提交按鈕的評級表。問題是當我訪問這個頁面時,出於某種原因它嘗試提交表單(當然,值爲'零')。驗證不會讓這樣做,所以它會呈現錯誤消息,這不是很好。當我訪問頁面時,Rails表格會自動提交
評級表:
= 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其餘的表單工作正常。
我不確定,但是這是你正在使用'find_or_create_by',這是馬上創建對象,嘗試find_or_initialize_by這個bcoz。或者你可以在你指定的函數中查找'function onload(){form0;};}; form.submit(); }指定。希望它可以幫助 – Sontya 2014-09-28 16:24:44
@Sontya,謝謝! find_or_initialize_by是答案!發佈它,我會提交它,謝謝! – 2014-09-28 17:01:07