beastlyCoder
beastlyCoder

Reputation: 2401

Redirecting submit button to the same page

could someone show me a way of making my button redirect to the same page that the submission took place. Here is what my form looks like:

<%= form_for Event.new do |f| %>
    <%= f.text_field :partycode %>
    <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>

Thanks in advance :)

Upvotes: 0

Views: 1101

Answers (1)

fbelanger
fbelanger

Reputation: 3578

This is a weird question, but I'm assuming you want the same view (i.e. page).

What you want to do is render the form again.

This is accomplished by overriding which view is rendered by your controller action.

def new # renders new.(format) by default
  @event = Event.new
end

def create
  @event = Event.new(event_params)

  if @event.save
    # ...
  else
    # ...
  end

  render 'new' # overrides default behaviour
end

Then assuming you have the following views:

# app/views/events/_form.html.erb
<%= form_for @event do |f| %>
  <%= f.text_field :partycode %>
  <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>

# app/views/events/new.html.erb
<%= render 'form' %>

Upvotes: 1

Related Questions