Ruby on Rails pass id to new create form

徘徊边缘 提交于 2019-12-22 10:52:50

问题


Ok, I've searched high and low, read tutorials, watched videos and I am still not getting any where with this. I've read similar questions here, but questions were more complex or lacked answers - so here goes...

I have models Account and Invoice. When showing an Account, I'd like a link to 'Create new invoice' which relates to that account. (Later I'd actually like a select field to choose an Account when creating an Invoice, but I'll leave that to another excruciation).

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's happening is, when I click on the New Invoice link it shows the new form, with the account field populated 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 is in the Invoices 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 is where I think I'm going wrong, but what to put this those lines is beyond me at the moment. I'm totally a beginner, any help to solve this functionality will surely teach me loads.

Thanks for your time.


回答1:


When you click the New invoice link on the /views/accounts/show page, I suppose that you want that your new invoice belongs to this account.

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

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

Also in the new action of your controller, replace @invoice = Invoice.new(:account_id => params[:account_id]) by @invoice = Invoice.new

Hope this helps.




回答2:


you did not post the code of your form, but i guess that you are using a text-field for handling the account association. THIS IS WRONG!

if you use a text-field, then rails will try storing it as a string => Account(#2281084000) expected, got String(#2267210740)

you need to use some kind of relational field like a dropdown or whatever to select one of the accounts that are already there.

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



来源:https://stackoverflow.com/questions/14569590/ruby-on-rails-pass-id-to-new-create-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!