1
votes

I'm using Rails 3, and I have a form_for in a StatusController. When I hit the submit button, my create method is never called. My create method has a redirect_to :index, however when I hit submit all of the information remains in the form, and the page does not redirect. The object does save correctly in the database however.

What would be causing this?

Controller:

class StatusController < ApplicationController
  def new
    @status = Status.new
  end
  def create
    @status = Status.new(params[:status])
    @status.date_added = Time.now
    if @status.save
    else
      render 'new'
    end
  end

View:

.well
  =form_for @status do |f|
    =f.label :user_email
    =f.text_field :user_email

    =f.label :added_by
    =f.text_field :added_by

    =f.label :comments
    =f.text_area :comments
    %br
    %br
    =f.submit

I've adjusted the code to this, and now the data disappears from the form upon a submit, however the object never gets saved because "Create" is never invoked.

2
@Goober You should post the code for your new and create actions from the status controller and also the form_for code from your 'new' view. - cdesrosiers
Actually, your create method does not have a redirect_to :index - dogenpunk
Do you have any validations in your Status model that might be causing this? - dogenpunk
If the create action really is not being called, perhaps it's a routing problem. Do you have resources :statuses in your routes.rb? - cdesrosiers
Thanks cdesrosiers, I'm an idiot... generated the controller as StatusController instead of StatusesController. No wonder everything was breaking. - Goober

2 Answers

0
votes

I'm just learning Ruby here, so I could be wrong, but it looks like you never redirect if the status is saved correctly.

 class StatusController < ApplicationController
  def new
    @status = Status.new
  end
  def create
    @status = Status.new(params[:status])
    @status.date_added = Time.now
    if @status.save
      format.html { redirect_to @status } # Or :index if you want to redirect to index
    else
      render 'new'
    end
  end

Of course, make sure you have those controller methods and views created as well.

0
votes

Your controller looks like a bit weird... I assume you have Rails 3.2 or newer.

class StatusController < ApplicationController
  respond_to :html

  def new
    @status = Status.new
  end
  def create
    @status = Status.new(params[:status])

    @status.date_added = Time.now
    @status.save
    respond_with(@status)
  end
end

respond_with is do all things for you. It renders new action if saving fails, and redirects to status_path(@status) if saving succeeds. If you want to change redirection behavior, you can use (otherwise undocumented) :location attribute to clarify, where you want to redirect user, or you can overwrite default "success" behavior by passing a block with one argument (format). See its documentation for more info.

Btw, if you use t.timestamps in status's migration, then you already have created_at field and it is handled automatically by save/update_attributes methods, so you do not need date_added.