问题
I am trying to use formtastic to make a form where I can enter an :opposition choose a :venue and a :team and then be presented with a list of players that I am able to check off to select them for the
I have got the form set up so it renders correctly however when submitted it does not save any information and just reloads the page.
My code is at my github here: https://github.com/jpknegtel/st_francis
models
This concerns the following models:
player
has_many :player_fixtures
has_many :fixtures, :through => :player_fixtures
fixtures
has_many :player_fixtures
has_many :players, :through => :player_fixtures
PlayerFixture
belongs_to :player
belongs_to :fixture
controller
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
form
<%= 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 %>
So when the form is submitted now nothing is passed. When looking at the web brick server nothing gets submitted but the page just gets reloaded.
It is possible to insert the records using rails console.
EDIT: I can now see this when submitted.
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
回答1:
My guess is massassignment. You need to allow rails to update some attributes via massassignment.
Add this line to your fixtures model:
attr_accessible :players_attributes, :opposition, :team_id, :venue_id, :date
This allows rails to set these attributes via new
and update_attributes
methods.
See the rails guide on security for more information.
来源:https://stackoverflow.com/questions/10261401/formtastic-rails-forms-not-submitting