2015-11-03 36 views
3

我有一個表單,我希望用戶回答每個問題,因此我添加了模型級驗證。 我假設它通過驗證,它應該重定向到另一個頁面調用「場景」(我還沒有完成它的視圖,所以它應該顯示模板丟失)。如果它沒有通過驗證,它應該再次渲染到新頁面,保留已填入的信息,並顯示驗證錯誤。form_for驗證不會正確地調用「創建」動作

但是,無論我填寫或不填寫,當我點擊提交按鈕時,它總是顯示索引頁面而不是「新」或「場景」(應該是模板缺失)。似乎忽略了我在「創造」行動中所寫的內容,而「創造」從未被稱爲。

我使用rails c並插入一條新記錄來測試驗證。它工作得很好,所以我猜這對我的模型和驗證沒有任何問題。

我也嘗試使form_for直接重定向到「場景」,以確保它對form_for很好地工作,並且它確實顯示了我缺少的模板,所以對於「創建」本身可能存在一些問題。我真的不知道發生了什麼問題。

<%= form_for @subject, :url => { :controller => 'appstores', :action => 'scenario' } do |f| %>

有一個類似的問題:rails form_for never invokes the create controller action to use redirect_to。 我曾嘗試使用「respond_with」,而不是工作。並檢查我的控制器命名爲appstroes_controller.rb資源:routes.rb中的應用程序。

我使用rails 4.2.4,ruby 2.0.0和bootstrap 3,不知道版本是否會導致這些問題。

任何幫助將不勝感激,謝謝!

應用程序/控制器/ appstores_controller.rb

class AppstoresController < ApplicationController 
    def index 
    end 

    def new 
     @subject = Subjectinfo.new 
    end 

    def create 
     @subject = Subjectinfo.new(params[:subjectinfo]) 
     if @subject.save 
      redirect_to :action => "scenario" 
     else # if not pass DB validation 
      render :action => :new 
     end 
    end 

    def scenario 
    end 
end 

應用程序/視圖/ appstores/new.html.erb

<%= form_for @subject, :url => { :controller => 'appstores', :action => 'create' } do |f| %>  
<form class="form-horizontal"> 
    <div class="form-group"> 
     <%= f.label :username, "User Name:", :class=>"control-label", :for=>"username" %> 
     <% if @subject.errors[:username].presence %> 
      <span class="model-error"><%= @subject.errors[:username].join(", ") %></span> 
     <% end %> 
     <%= f.text_field :username, :autocomplete=>"off", :placeholder=>"User Name", :class=>"form-control", :id=>"username" %> 
    </div> 
    <div class="form-group"> 
     <%= f.label :mobile_user, "Are you a mobile device user?", :class=>"control-label", :for=>"mobile_user" %> 
     <% if @subject.errors[:mobile_user].presence %> 
      <span class="model-error"><%= @subject.errors[:mobile_user].join(", ") %></span> 
     <% end %> 
     <div class="radio radio-primary"> 
      <%= f.radio_button :mobile_user, "1", :id=>"mobile_user_1" %> 
      <%= f.label :mobile_user, "Yes", :class=>"control-label", :for=>"mobile_user_1" %> 
     </div> 
     <div class="radio radio-primary"> 
      <%= f.radio_button :mobile_user, "0", :id=>"mobile_user_0" %> 
      <%= f.label :mobile_user, "No", :class=>"control-label", :for=>"mobile_user_0" %> 
     </div> 
    </div> 
     <div class="text-center"> 
      <%= f.submit "NEXT", :class => "btn btn-default btn-outline btn-lg" %> 
     </div> 
</form> 
<% end %> 

應用程序/ MODLE/subjectinfo.rb

(支持 「attr_accessible」 鋼軌4,我已經把我的Gemfile 「寶石 'protected_attributes'」)

class Subjectinfo < ActiveRecord::Base 
     validates_presence_of :username, :mobile_user 
     attr_accessible :username, :mobile_user 
    end 

的config/routes.rb中

AppStore::Application.routes.draw do 
    match ':controller(/:action(/:id(.:format)))', :via => :all 
    root :to => "appstores#index" 
    resources :appstores 
    get "appstores/scenario"=>"appstores#scenario" 
end 

耙路線

   Prefix Verb URI Pattern       Controller#Action 
          /:controller(/:action(/:id(.:format))) :controller#:action 
       root GET /         appstores#index 
      appstores GET /appstores(.:format)     appstores#index 
        POST /appstores(.:format)     appstores#create 
     new_appstore GET /appstores/new(.:format)    appstores#new 
     edit_appstore GET /appstores/:id/edit(.:format)   appstores#edit 
      appstore GET /appstores/:id(.:format)    appstores#show 
        PATCH /appstores/:id(.:format)    appstores#update 
        PUT /appstores/:id(.:format)    appstores#update 
        DELETE /appstores/:id(.:format)    appstores#destroy 
appstores_rfscenario GET /appstores/rfscenario(.:format)  appstores#rfscenario 

順便說一句,這是我看到的形式終端,這是當我填寫所有領域。

Started POST "/appstores" for 127.0.0.1 at 2015-11-03 22:25:54 +0800 
Processing by AppstoresController#index as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"7WV4Iw/0uHNSnuXpr8qa39oFEF9gZfKm8EyHGQna0o0=", "subjectinfo"=>{"username"=>"a1", "mobile_user"=>"1"}, "commit"=>"NEXT"} 
    Rendered appstores/index.html.erb within layouts/application (12.6ms) 
Completed 200 OK in 91ms (Views: 88.0ms | ActiveRecord: 0.0ms) 

這裏是當我把它們留空,但不管它是否爲空,它總是呈現索引......

Started POST "/appstores" for 127.0.0.1 at 2015-11-03 22:25:54 +0800 
Processing by AppstoresController#index as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"7WV4Iw/0uHNSnfuXpr8qa39oFEF9gZfKm8EyHGQna0o0=", "subjectinfo"=>{"esearch"=>""}, "commit"=>"NEXT"} 
    Rendered appstores/index.html.erb within layouts/application (12.6ms) 
Completed 200 OK in 91ms (Views: 88.0ms | ActiveRecord: 0.0ms) 
+0

您正在使用哪種版本的導軌? – Pavan

+0

它不會重定向到索引,呈現新動作意味着它將呈現新視圖,而創建動作位於URL'/ appstores'下,如您在路由中所看到的,只有一個是獲取請求,另一個是發佈請求。 –

+0

@Pavan,我使用rails 4.2.4和ruby 2.0.0,會導致版本問題?我正在嘗試創建另一個項目並將其降級到Rails 3.2.14,並看看我現在是否也遇到了同樣的問題。 –

回答

1

routes.rb文件中刪除線match ':controller(/:action(/:id(.:format)))', :via => :all而從不使用它了。這種模式匹配任何路線,並且它位於第一個位置,因此文件中的其他路線根本就沒有機會。

+0

它確實有幫助!謝謝!!! 不知道我以前的項目如何在這條線上存在。 –

相關問題