In my friends controller I have the following
shutil.move(os.path.join(root, file_name), 'dest\path')
which essentially saves a def create
@friends_array = Array.new
@bill = Bill.find(params[:bill_id])
@friend = @bill.friends.build(friend_params)
if @friend.save
@friends_array << @friend
redirect_to bill_path(@bill)
else
redirect_to root_path
end
end
that contains a list of names that I can use in my views.
I have three models in my application, a @friends_array
, a Bill.rb
, and a Transaction.rb
. I am using MongoDB (Mongoid) in this application.
Friend.rb
In my view, I simply want to set the class Bill
include Mongoid::Document
field :event_name, type: String
field :urlID, type: String
# creates a urlID that the users can refer to afterwards
field :_id, type: String, default: ->{ urlID }
# contains many transactions and friends
embeds_many :transactions
embeds_many :friends
#validates the uniqueness of urlID
validates_uniqueness_of :urlID
end
class Transaction
include Mongoid::Document
embedded_in :bill
field :payer, type: String
field :dollar, type: Integer
field :cent, type: Integer
end
class Friend
include Mongoid::Document
embedded_in :bill
field :name, type: String
end
field in the Transaction Model to one of the names within the :payer
, so I have this on the show page of each bill.
@friends_array
I can't seem to get <h2>New Transaction</h2>
<%= form_for [@bill, Transaction.new] do |f| %>
<p><%= f.label :payer %> <%=f.collection_select(:id, @friends_array, :id, :name)%></p>
<p><%= f.label :dollar %> <%= f.text_field :dollar %></p>
<p><%= f.label :cent %> <%= f.text_field :cent %></p>
<p><%= f.submit %></p>
<% end %>
working. The current one returns a collection_select
map' for nil:NilClass` Error.
If I try undefined method
it returns a <%=f.collection_select(:friend_id, @friends_array, :id, :name)%></p>
friend_id'` Error.
If I try undefined method
it returns a <%=f.collection_select(:bill_id, @friends_array, :id, :name)%>
bill_id'` Error.
What is the correct syntax for what I want to achieve?
答案 0 :(得分:1)
未定义的方法错误来自于期望用于创建它的对象的方法friend_id
- 在本例中为Transaction.new
。您正在使用的方法将生成一个字段,其最初选择的值是friend_id
实例的Transaction
的当前值,但显然您的模型没有此列。您需要将列friend_id
添加到Transaction
模型:
rails generate migration AddFriendIdToTransactions friend:references
rake db:migrate
或者您需要为此字段使用表单字段标记,而不是表单对象方法:
<%= collection_select_tag(:transaction, :id, @friends_array, :id, :name) %>