views:

40

answers:

2

This might be impossible to answer since there are probably too many variables here, but I thought I'd give it a shot since I always find other answers here. I am still fairly new to rails.

So, I have a bills model/controller/view. I want to create a new bill. I will be editing out the things that shouldn't matter much, but if they are needed I can add them in - just don't want a wall of text.

In the route:

map.resources :bills

My new method in the controller:

def new
    @bill = Bill.new
    @submit_txt = "Create"

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @bill }
    end
  end

my form:

<% form_for(@bill) do |f| %>
        <%= f.error_messages %>
 ### form elements here, this all seems fine ####
        <p>
          <%= f.submit @submit_txt %>
        </p>
    <% end %>

my create method in the controller:

def create
    is_weekly = false
    is_monthly = false

    @bill = current_user.recurring_bills.build(params[:bill])
    @bill.year = @current_year

    @errors = 'checking this out'

    if @errors.blank?
      logger.info "no errors, supposedly; going to save"

     ### do saving stuff here####
    else
      logger.info "errors not blank"
      render :action => :new
    end
end

For some reason this always renders /bills instead of /bills/new. It used to work and I don't know what I did wrong, but now it's not. I get the same response with render :template => 'bills/new'. It goes to the right page with a redirect, but then it won't fill in the form with old values.

The log:

Processing BillsController#create (for 127.0.0.1 at 2010-07-21 21:00:47) [POST]
  Parameters: {"commit"=>"Create", "action"=>"create", "authenticity_token"=>"Kc7/iPKbfJBKHHVARuN7K6207tW6Jx4OUn7Xb4uSB8A=", "bill"=>{"name"=>"rent", "month"=>"", "amount"=>"200", "alternator"=>"odd", "day"=>"35", "frequency"=>"monthly", "weekday"=>""}, "controller"=>"bills"}
  User Load (0.6ms)   SELECT * FROM "users" WHERE ("users"."remember_token" = 'dd7082c56f5a252d14e4e68c528eb26551875c647f998c15d16a064cb075d63c') LIMIT 1
errors not blank
Rendering template within layouts/application
Rendering bills/new
Rendered bills/_form (14.5ms)
Rendered layouts/_stylesheets (3.3ms)
Rendered layouts/_header (5.7ms)
Rendered layouts/_footer (0.3ms)
Completed in 174ms (View: 30, DB: 1) | 200 OK [http://localhost/bills]

Hopefully someone has an idea of what I've done wrong, or I guess I'm starting over.

A: 

Try this:

render :new

From the docs:

Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render.

Give this a shot and let us know how it goes. Also, if you render "new", remember that your new action creates a new Bill object and there won't be any old values for it to fill in. I think what you really want to do is render :edit. And in your edit action find the Bill object with the params that you pass to the action.

sosborn
Hm, it's still having the same problem. Maybe I am just doing this the wrong way - the idea here is that the person is trying to create a new bill, but something went wrong so it's rendering the new page again with the info they already entered. Should this be the edit page? I assumed that would be for items already saved in the database.
d3vkit
A: 

run a rake:routes from your command line and you will see how they map.

    bills GET    /bills(.:format)          {:controller=>"bills", :action=>"index"}
          POST   /bills(.:format)          {:controller=>"bills", :action=>"create"}
 new_bill GET    /bills/new(.:format)      {:controller=>"bills", :action=>"new"}
edit_bill GET    /bills/:id/edit(.:format) {:controller=>"bills", :action=>"edit"}
     bill GET    /bills/:id(.:format)      {:controller=>"bills", :action=>"show"}
          PUT    /bills/:id(.:format)      {:controller=>"bills", :action=>"update"}
          DELETE /bills/:id(.:format)      {:controller=>"bills", :action=>"destroy"}

The RESTful resources take a little getting used to but in your case \bills with a post method goes to the create action. You are specifying in the create action to render the contents of the new template when you call render :action => :new - you do not actually run the action.

Geoff Lanotte
Both answers given were very helpful but I didn't know about rake:routes, and this explains pretty nicely the order of what's happening. Thanks!
d3vkit