I am confused with this. I have a company model and a message model. Company has_many messages and messages belongs_to company. I am having problems with the data being saved to the proper company with this form is saved.
<%= form_for(@msg) do |f| %>
<%= render 'errors', :object => f.object %>
<ul class="fields">
<li>
<%= select("msg", "company_id", Company.all.collect {|p| [ p.title, p.id ] }) %>
</li>
</ul>
<ul class="fields">
<li><%= f.label :content, "Send this company your message" %></li>
<li><%= f.text_area :content %></li>
</ul>
<div id="actions">
<%= f.submit "Send" %>
</div>
<% end %>
every time this form is saved the company_id is null. My code for the controller to save is
def create
@msg = current_user.messages.build(params[:msg])
if @msg.save
flash[:success] = "New message saved"
redirect_to current_user
else
rende开发者_JS百科r current_user
end
end
Nothing stands out to me as to why this is not saving, can someone guide me to the right direction?
Jeff
In your controller, don't you need to find the current company? Something like
@company = Company.find(params[:company_id])
@message = @Company.messages.create(params[:msg])
would work. As far as mixing has_many with both users and companies, this thread on SO seems good. Rails Model has_many with multiple foreign_keys
精彩评论