我有一个控制器#new,用作根路径。
路由.rb
resources :participants
root :to => 'participants#new'
耙路线
participants GET /participants(.:format) participants#index
POST /participants(.:format) participants#create
new_participant GET /participants/new(.:format) participants#new
edit_participant GET /participants/:id/edit(.:format) participants#edit
participant GET /participants/:id(.:format) participants#show
PUT /participants/:id(.:format) participants#update
DELETE /participants/:id(.:format) participants#destroy
root / participants#new
这在访问 xxx.xx/ 时效果很好
但是当我在控制器中渲染 #new 时,我被重定向到 /participants ,我该如何阻止这种情况发生?
def create
@participant = Participant.new(params[:participant])
respond_to do |format|
if @participant.save
format.html { redirect_to root_path, notice: "<h2>Tack!</h2> <p>Registrering genomförd, vi har skickat ut ett mail med instruktioner till #{@participant.email}</p>".html_safe }
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
日志:
Started POST "/participants" for 127.0.0.1 at 2013-03-13 13:21:29 +0100
Processing by ParticipantsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"dXmuTX/ugwgNjc21PPdiSHDGlNXEEGZCRHVIWKELOuw=", "participant"=>{"company"=>"asd", "f_name"=>"asd", "l_name"=>"asd", "email"=>"asd@asd.com", "phone_number"=>"asd", "allergy"=>"asd"}, "commit"=>"Anmäl mig!"}
MOPED: 127.0.0.1:27017 COMMAND database=damn_development command={:count=>"models", :query=>{"company"=>"asd", "_type"=>{"$in"=>["Participant"]}}} (0.7780ms)
MOPED: 127.0.0.1:27017 QUERY database=damn_development collection=models selector={"email"=>"asd@asd.com", "_type"=>{"$in"=>["Participant"]}} flags=[] limit=1 skip=0 batch_size=nil fields={:_id=>1} (0.5569ms)
Rendered participants/_form.html.erb (4.7ms)
Rendered participants/new.html.erb within layouts/application (5.5ms)
Completed 200 OK in 25ms (Views: 19.8ms)