【问题标题】:Formtastic / rails forms not submittingFormtastic / rails表单未提交
【发布时间】:2012-04-21 17:37:30
【问题描述】:

我正在尝试使用 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

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

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

可以使用 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

【问题讨论】:

  • 该仓库中没有“semantic_form_for”。你提交了所有最新的东西吗?
  • 我刚刚推送了提交。它现在应该在那里,对此感到抱歉。
  • 您可以发布您的控制器new 操作的代码吗?你在那里分配@fixture 吗?
  • 尝试使用semantic_form_for @fixture...
  • 已放入新控制器,收到以下错误 undefined method fixtures_path' for #:0x482be00>`

标签: ruby-on-rails has-and-belongs-to-many formtastic


【解决方案1】:

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

将此行添加到您的灯具模型中:

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

这允许 rails 通过new 和update_attributes 方法设置这些属性。

请参阅rails guide on security 了解更多信息。

【讨论】:

  • 我已经添加了以下内容,但是我似乎无法做任何不同的事情。仍然会立即重新加载。但是现在已经看到了:参见上面的编辑
  • 您能再发布一些日志吗?我想看看表单请求的日志输出,然后是提交表单时的输出。
  • 添加了更多内容。老实说,我看不出还有什么用处?
  • 但这不是你提交的时候。这仅适用于表格的请求。您可以在单击按钮时添加日志输出吗? (对于/fixtures/create)。
  • 好的 /create 现在可以工作了。谢谢你。 (我知道它应该是 /new 但是现在就可以了)
猜你喜欢
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多