简体   繁体   中英

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| and rewriting it, however that didn't change anything. Neither did rewriting the 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

format.html { redirect_to @event, notice: "Event was successfully created.", status: :see_other }

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM