2015-04-16 47 views
0

所以我有3個級聯下拉式和一個小船模型(brand_id,year_id,model_id)。錯誤沒有顯示。我有補充;下拉列表錯誤信息未顯示

validates :brand_id, presence: true 
    validates :year_id, presence: true 
    validates :model_id, presence: true 
    validates :user_id, presence: true 

#boat.rb。我有我的形式;

<%= form_for(@boat) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
     <div class="col-md-6"> 
     <%= f.label :Brand %> 
     <%= f.collection_select(:brand_id, @brands, :id, :name, {:prompt => "Select a Brand"}, {:id => 'brands_select'}) %> 
    </div> 
    <div class="col-md-6"> 
     <%= f.label :Year %> 
     <%= f.collection_select(:year_id, @years, :id, :name, {:prompt => "Select a Year"}, {:id => 'years_select'}) %> 
     </div> 
     <div class="col-md-6"> 
     <%= f.label :Model %> 
     <%= f.collection_select(:model_id, @models, :id, :name, {:prompt => "Select a Model"}, {:id => 'models_select'}) %> 
    </div> 
     <div class="col-md-6 col-md-offset-3"> 
     <%= f.submit "Next", class: "btn btn-primary"%> 
    </div> 
    <% end %> 

和我有shared/errors

​​

該共享的錯誤消息完全適用於用戶模型。而我的#create動作

def create 
    @boat = current_user.boats.build(boat_params) if logged_in? 
    if @boat.save 
     flash[:success] = "Boat created!" 
     redirect_to root_url 
    else 
     redirect_to new_boat_path(current_user) #returns here 
    end 
    end 

我打電話給new.html並打開窗體。當我按沒有選擇任何東西時,它會重定向到new_boat_path(current_user)而不會顯示錯誤。我不知道爲什麼。

EDIT1: 這裏是日誌:

Started GET "/boats/new.2" for 88.240.3.128 at 2015-04-16 21:58:01 +0000 
Processing by BoatsController#new as 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]] 
    Rendered shared/_error_messages.html.erb (0.0ms) 

所以它呈現,但我沒有看到的頁面

EDIT 2上的任何錯誤:#new方法(品牌,年份&模型級聯降下拉列表,其中船模型都有自己的ID)

def new 
    @boat = Boat.new 
    @brands = Brand.all 
    @years = Year.all 
    @models = Model.all 
    end 

回答

1

您是redirectingnew_boat_pathnew操作會創建一個新實例Boat,該實例顯然不會包含任何錯誤。

你可能想要做的是:

if @boat.save 
    flash[:success] = "Boat created!" 
    redirect_to root_url 
else 
    render 'new' 
end 

這將不執行重定向,但只是呈現new視圖其中包含@boat變量包括通過調用@boat.save創建的驗證錯誤。

+0

我試過了,但後來出現錯誤:BoatsController中的'NoMethodError#create''nil:NilClass''#的undefined方法'map應該會產生所需的結果。 def options_from_collection_for_select(collection,value_method,text_method,selected = nil) options = collection.map do | element | [value_for_collection(元件,text_method),value_for_collection(元件,value_method),option_html_attributes(元件)]中選擇,殘障人士= extract_selected_and_disabled(選擇的) 端 ' – Shalafister

+0

是否有在'new'方法的任何特定代碼? –

+0

我添加了新方法 – Shalafister