0

I want to create an event by users using multiple steps for the creation. At the moment I have the following problem that it redirects to the first step after completing the second step.

The following is my controller

def new 
  @event = Event.new(params[:event])
  @user = current_user
  respond_to do |format|
    if @event.save
      format.html { render :template => "events/new2" }
      format.json { render json: event_new2, status: :created, location: @event }
    else
      format.html { render action: "new" }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

def event_new2
  @event = Event.find(params[:id])
  respond_to do |format|
    if @event.update_attributes(params[:event])
      format.html { render :template => "events/new3" }
      # format.json { render json: event_new2, status: :created, location: @event }
    else
      format.html { render action: "event_new2" }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

 # POST /events
 # POST /events.json
def create
  @event = Event.new(params[:event])
  @user = current_user
  respond_to do |format|
    if @event.save
      format.html { redirect_to @event, notice: 'event erfolgreich erstellt.' }
      format.json { render json: @event, status: :created, location: @event }
    else
      format.html { render action: "new" }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

My model requires only title and description to be present. The views are as follows:

Step1

<%= simple_form_for (@event), :method => 'new' do |f| %>  
  <%= f.error_notification %>
  <%= f.input :title,:label => 'Event Titel' ,:placeholder=>"Titel des Events"%>
  <%= f.text_area :description,:label => 'Beschreibung',:placeholder=>"Beschreibung"%>
  <%= f.submit "Event erstellen", class: "btn btn-large btn-primary pull-right"%>

Step2

<%= simple_form_for (@event), :method => 'new' do |f| %>  
  <%= f.error_notification %>
  <%= f.input :category%>
  <%= f.input :website, :label=>"Website"%>

Any help? Thanks in advance.

danivovich
  • 4,197
  • 4
  • 30
  • 31
user1884013
  • 67
  • 1
  • 5

1 Answers1

0

The normal one step method to create a model is for someone to go to the new page which will call the new controller and render the new page. The user fills in a form and hits submit which drives the create controller. If that works, it redirects to the show page.

If I were to do a two step creation, I would probably still go to the new page first and have it call the create action, but then rather than saving the model, I'd render the step2 page and have them fill in more details and also include the first page of details in hidden fields. I would have that form call a different action than the first at which point I would save the model. Conversely, I could have the first form call the step2 action then have that page call the create action. Another option is to save the model after step1 and retrieve it on step2. This option would be kind of ugly if step1 succeeds, but step2 fails.

Your new controller as it is will save a model to the database (without getting any user input) then render the new2 page. I highly doubt this is what you want. If it is, I'd highly recommend against it.

I assume Step1 is what you render for new2 and Step 2 is what you're rendering for new3. However, both of these forms as is will call the update controller (not shown). Also the method new is not standard, I'm a little surprised it didn't choke on it. Standard methods are :get, :post, :put, and :delete. You may be able to create non-standard one in your routes configuration, but I wouldn't recommend it. If you want to direct your form to your new2 controller then you'll need to setup a route and use the :url option to form_for to get it there.

I hope that helps.

Geoff
  • 2,208
  • 1
  • 16
  • 17