Ruby on Rails passes id to new create form

Ok I have searched high and low, read tutorials, watched videos and I still don't understand where with this. I read similar questions here, but the questions were more difficult or there were no answers - so here it goes ...

I have an account and an invoice. When you show an account, I need a "Create New Account" link associated with that account. (Later, I really would have liked to select the account selection field when creating the invoice, but I'll leave that to another torment.)

Here are my models ... Account:

class Account < ActiveRecord::Base
  attr_accessible :name, :invoice 
  attr_accessible :name, :invoice
  has_many :invoices
end

      

and invoice:

class Invoice < ActiveRecord::Base
  belongs_to :account
  attr_accessible :amount_pretax, :amount_total, :date_sent, :project, :status, :tax, :account, :account_id
end

      

Now, in my /views/accounts/show.html.erb

<p id="notice"><%= notice %></p>
<p>
  <b>Name:</b>
  <%= @account.name %>
</p>
<%= link_to 'New Invoice', new_invoice_path(:account_id=>@account.id) %>
<%= link_to 'Edit', edit_account_path(@account) %> |
<%= link_to 'Back', accounts_path %>

      

So what happens when I click the New Invoice link, it shows a new form where the account field is filled with this weird text: #<Account:0x10fe16bc0>

and then when I submit the form I get this error: ActiveRecord :: AssociationTypeMismatch in InvoicesController # create with this statement: Account(#2281084000) expected, got String(#2267210740)

along with this:

app/controllers/invoices_controller.rb:45:in `new'
app/controllers/invoices_controller.rb:45:in `create'

      

This is what's in the invoice controller:

def new
  @invoice = Invoice.new(:account_id => params[:account_id])
  respond_to do |format|
  format.html # new.html.erb
  format.json { render :json => @invoice }
  end
end

def create
  @invoice = Invoice.new(params[:invoice])
   ....
end

      

The above was what I think I am wrong, but what to put these lines is now outside of me. I am completely newbie, any help to solve this feature will surely teach me loads.

Thank you for your time.

+3


source to share


2 answers


When you click a link New invoice

on a page /views/accounts/show

, I assume that you want your new account to belong to that account.

So, in your form, you don't need to let the user choose an account. You can, for example, replace the corresponding field with hidden_field

:

<%= f.hidden_field :account_id, :value => params[:account_id] %>

      



Also in new

your controller action , replace @invoice = Invoice.new(:account_id => params[:account_id])

with@invoice = Invoice.new

Hope it helps.

+2


source


you have not posted your form code, but I am assuming you are using a textbox to handle the association account

. IT IS NOT RIGHT!

if you are using a textbox then rails will try to store it as a string => Account(#2281084000) expected, got String(#2267210740)



you need to use some kind of relational field like dropdown menu or whatever to select one of the already existing accounts.

there are many good examples, this might help you: http://railscasts.com/episodes/102-auto-complete-association-revised

0


source







All Articles