I have developed a new form that I would like to render using ajax in case it fails validations.
In my controller, the code looks like this:
def create
event = CEvent.new(params[:c_event])
respond_to do |format|
if event.save
format.html { redirect_to admin_c_events_path }
else
format.js {redirect_to new_admin_c_event_path(:event => event.as_json, :errors => event.errors )}
end
end
end
The form has the parameter :remote => true
, and when I submit the form by clicking on the button, the form is submitted via JavaScript but nothing gets rendered. However, when I reload the page, the required information renders correctly. If I reload the page again, an empty new form appears.
This is what my console output looks like:
Started GET "/admin/c_events/new?errors=%23%3CActiveModel%3A%3AErrors%3A0x007f74780d1d50%3E&event%5Babout%5D=&event%5Bbackground_content_type%5D=&event%5Bbackground_file_name%5D=&event%5Bbackground_file_size%5D=&event%5Bbackground_updated_at%5D=&event%5Bcontact_person_id%5D=11&event%5Bcreated_at%5D=&event%5Bfinish_at%5D=2014-01-13+11%3A19%3A00+UTC&event%5Bid%5D=&event%5Blocation%5D=&event%5Bname%5D=dewqr&event%5Bpress%5D=&event%5Bpress_contact_person_id%5D=11&event%5Bstart_at%5D=2014-01-13+11%3A19%3A00+UTC&event%5Bupdated_at%5D=&event%5Bvideo%5D=" for 127.0.0.1 at 2014-01-13 13:19:43 +0200
Processing by Admin::CEventsController#new as JS
Parameters: {"errors"=>"#<ActiveModel::Errors:0x007f74780d1d50>", "event"=>{"about"=>"", "background_content_type"=>"", "background_file_name"=>"", "background_file_size"=>"", "background_updated_at"=>"", "contact_person_id"=>"11", "created_at"=>"", "finish_at"=>"2014-01-13 11:19:00 UTC", "id"=>"", "location"=>"", "name"=>"dewqr", "press"=>"", "press_contact_person_id"=>"11", "start_at"=>"2014-01-13 11:19:00 UTC", "updated_at"=>"", "video"=>""}}
Person Load (1.2ms) SELECT "people".* FROM "people" WHERE (p_type LIKE '%member%')
Person Load (0.5ms) SELECT "people".* FROM "people" WHERE (p_type LIKE '%mentor%')
Rendered admin/c_events/_select_speakers.html.erb (4.0ms)
Startup Load (0.4ms) SELECT "startups".* FROM "startups"
Rendered admin/c_events/_select_startups.html.erb (4.6ms)
Rendered admin/c_events/_schedule_fields.html.erb (13.8ms)
CACHE (0.0ms) SELECT "people".* FROM "people" WHERE (p_type LIKE '%member%')
Rendered admin/c_events/new.html.erb within layouts/admin (116.0ms)
Rendered layouts/_header.html.erb (3.3ms)
Rendered admin/_top_bar.html.erb (4.8ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 10216ms (Views: 180.0ms | ActiveRecord: 2.1ms)
Is there a way to render a view without reloading the page?