我正在创建一个允许您安排事件的网站,当创建一个新事件时,它被成功地添加到数据库中,控制台将输出它被重定向到查看事件,但是似乎没有重定向发生。我试过删除respond_to do |format|并重写它,但是这并没有改变任何东西。也没有重写重定向到redirect_to.
控制台输出
rails_1 | Event Create (0.8ms) INSERT INTO `events` (`username`, `date_made`, `date_for`, `title`, `attendees`, `owner_id`, `created_at`, `updated_at`, `description`, `is_all_day`, `color`, `text_color`) VALUES ('screamingatrocks@proton.me', '2022-06-26 21:12:17', '2022-06-08 00:00:00', '', x'', 13, '2022-06-26 21:12:42.625491', '2022-06-26 21:12:42.625491', '', FALSE, '#000000', '#000000')
rails_1 | ↳ app/controllers/events_controller.rb:30:in `block in create'
rails_1 | PublicActivity::Activity Create (0.8ms) INSERT INTO `activities` (`trackable_type`, `trackable_id`, `key`, `created_at`, `updated_at`) VALUES ('Event', 35, 'event.create', '2022-06-26 21:12:42', '2022-06-26 21:12:42')
rails_1 | ↳ app/controllers/events_controller.rb:30:in `block in create'
rails_1 | TRANSACTION (12.1ms) COMMIT
rails_1 | ↳ app/controllers/events_controller.rb:30:in `block in create'
rails_1 | Redirected to https://localhost/events/35
rails_1 | Completed 200 OK in 43ms (ActiveRecord: 15.2ms | Allocations: 5294)
事件控制器
# POST /events or /events.json
def create
@event = Event.new(event_params)
respond_to do |format|
if @event.save
UserMailer.event_reminder(current_user)
format.html { redirect_to @event, notice: "Event was successfully created." }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
形式<%= form_with(model: event) do |form| %>
...
<div class="actions">
<%= form.submit %>
</div>
如果你正在使用turbo,你需要返回状态303 turbo知道你想重定向,所以你需要添加status: :see_other
到redirect_to
format.html { redirect_to @event, notice: "Event was successfully created.", status: :see_other }
引用:
- https://turbo.hotwired.dev/handbook/drive redirecting-after-a-form-submission
- https://discuss.rubyonrails.org/t/why -涡轮-休息-所有- 302 redirects/79810/3