ルート パスとして使用する controller#new があります。
Routes.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)