2012-04-21 106 views
1

我想用formtastic製作一個表單,我可以輸入一個:反對選擇一個:場地和一個:球隊,然後顯示一個我可以檢查的球員列表選擇他們的Formtastic/rails表單不提交

我已經得到了表格設置,所以它呈現正確,但是當提交它不保存任何信息,只是重新加載頁面。

我的代碼是在我的github上的位置:https://github.com/jpknegtel/st_francis

車型

這涉及以下型號:

球員

has_many :player_fixtures 
has_many :fixtures, :through => :player_fixtures 

燈具

has_many :player_fixtures 
has_many :players, :through => :player_fixtures 

PlayerFixture

belongs_to :player 
belongs_to :fixture 

控制器

def create 
@fixture = Fixture.new(params[:fixture]) 
if @fixture.save 
    flash[:notice] = "Fixture Created" 
    redirect_to(:action =>'list') 
else 
    flash.now[:error] = "Could not save fixture. Please re-enter information" 
    render('new') 
end 
end 

def new 
@fixture = Fixture.new 
end 

形式

<%= semantic_form_for :fixture do |f| %> 
<%= f.inputs do %> 
    <%= f.input :opposition %> 
    <%= f.input :team, :as => :select, :collection => Team.all %> 
    <%= f.input :venue, :as => :check_boxes, :collection => Hash[Venue.all.map{|b| [b.name, b.id]}]%> 
    <%= f.input :players, :as => :check_boxes, :collection => Hash[Player.all.map{|b| [b.full_name, b.id]}], :required => true %> 
<% end %> 
<%= f.actions do %> 
<%= f.action :submit, :as => :button %> 
<%= f.action :cancel, :as => :link %> 
<% end %> 
<% end %> 

所以當表單提交現在沒有通過。在查看Web Brick服務器時,沒有提交任何內容,但頁面只是重新加載。

可以使用導軌控制檯插入記錄。

編輯:我現在可以在提交時看到這個。

Started POST "/fixtures/new" for 127.0.0.1 at 2012-04-23 15:00:21 +0100 
Processing by FixturesController#new as HTML 
Parameters: {"utf8"=>"✓",  "authenticity_token"=>"Hx4TChWiUdhpZbAfgWUYMWBKao86pZh0tGzwVKy+P80=", "fixture"=> {"opposition"=>"Mid sussex", "team"=>"1", "venue"=>["", "1"], "players"=>["", "1", "3"]}, "button"=>""} 
Team Load (1.0ms) SELECT `teams`.* FROM `teams` 
Venue Load (1.0ms) SELECT `venues`.* FROM `venues` 
Player Load (1.0ms) SELECT `players`.* FROM `players` 
Rendered fixtures/new.html.erb within layouts/application (173.0ms) 
Completed 200 OK in 200ms (Views: 163.0ms | ActiveRecord: 36.0ms) 
[2012-04-23 15:00:21] WARN Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true 
+0

該回購中沒有'semantic_form_for'。你有沒有提交所有最新的東西? – 2012-04-21 19:20:30

+0

我剛推送提交。它現在應該在那裏,對此感到遺憾。 – JPKnegte 2012-04-22 12:27:47

+0

你可以發佈你的控制器的'新'行動的代碼?你在那裏分配'@ fixture'嗎? – Brandan 2012-04-22 15:37:38

回答

2

我的猜測是massassignment。您需要允許rails通過批量更新某些屬性。

此行添加到您的燈具型號:

attr_accessible :players_attributes, :opposition, :team_id, :venue_id, :date 

這使導軌通過newupdate_attributes方法來設置這些屬性。

查看rails guide on security瞭解更多信息。

+0

我已經添加了以下內容,但是我似乎無法做任何不同的事情。仍然會立即重新加載。但現在看到了這一點:請參閱上面的編輯 – JPKnegte 2012-04-22 15:25:29

+0

您可以發佈一些更多的日誌?我希望查看錶單請求的日誌輸出,以及提交表單時的輸出。 – klump 2012-04-22 20:58:33

+0

增加了一些。說實話,我看不到其他用途? – JPKnegte 2012-04-23 14:21:15