1

我正在尝试使用 formtastic 制作一个表格,我可以在其中输入一个 :opposition 选择一个 :venue 和一个 :team ,然后显示一个玩家列表,我可以勾选这些列表以选择他们

我已经设置了表单,因此它可以正确呈现,但是在提交时它不会保存任何信息,只是重新加载页面。

我的代码在我的 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

播放器夹具

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 %>

因此,现在提交表单时,什么都没有传递。当查看网络砖服务器时,没有任何内容被提交,但页面只是被重新加载。

可以使用 rails 控制台插入记录。

编辑:我现在可以在提交时看到这个。

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
4

1 回答 1

2

我的猜测是质量分配。您需要允许 rails 通过批量分配更新某些属性。

将此行添加到您的固定装置模型中:

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

这允许 rails 通过newupdate_attributes方法设置这些属性。

有关更多信息,请参阅有关安全性的 rails 指南

于 2012-04-22T14:30:59.503 回答