[英]Ruby On Rails Not Redirecting On Create
I am creating a website that allows you to schedule events, when creating a new event it is added successfully to the database and the console will output that it's redirected to view the event, however no redirect seems to ever occur.我正在创建一个允许您安排事件的网站,当创建一个新事件时,它会成功添加到数据库中,并且控制台将输出它被重定向以查看事件,但是似乎没有发生重定向。 I've tried removing the respond_to do |format|我试过删除 respond_to do |format| and rewriting it, however that didn't change anything.并重写它,但这并没有改变任何东西。 Neither did rewriting the redirect to redirect_to.也没有将重定向重写为redirect_to。
Console output控制台输出
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)
Events Controller事件控制器
# 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形式
<%= form_with(model: event) do |form| %>
...
<div class="actions">
<%= form.submit %>
</div>
If you are using turbo, you need to return status 303 to turbo know that you want to redirect, so you need to add status: :see_other
to redirect_to如果使用的是turbo,需要返回status 303给turbo才知道要重定向,所以需要在redirect_to中加上status: :see_other
format.html { redirect_to @event, notice: "Event was successfully created.", status: :see_other }
References:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.