Rails3 没有将第二个 id 添加到我的表中
我遇到的问题是 company_id 没有保存到详细信息表中 我知道 company_id 在那里,我使用 <%= debug(params[:id])%> 检查它在添加所有公司详细信息之前,在表单上添加所有公司详细信息,但由于某种原因,保存了除 company_id 之外的所有其他内容,
##user.rb
has_one :company
##company.rb
belongs_to :user
has_one :detail
##detail.rb
belongs_to :user
##details controller
def new
@detail = Detail.new
user_id = session[:user_id]
company_id = params[:id]
end
def create
@detail = Detail.new(params[:detail])
@detail.user_id = session[:user_id]
@detail.company_id = params[:id]
end
###settings.html.erb
### this is where i make sure that company_id gets pass with the url
link_to 'New Detail', {:controller => 'details', :action =>'new', :id => company.id }, :class => 'plus'
#####routes
resources :details
resources :companies
resources :users
resources :sessions
我知道这可能看起来不太漂亮或不合适,如果您知道更好的方法,请告诉我...提前致谢。
the problem I'm having is company_id is not been save to the details table
I know the company_id is there I check it using <%= debug(params[:id])%> on the form before adding all company details information but for some reason is saving everything else but the company_id
##user.rb
has_one :company
##company.rb
belongs_to :user
has_one :detail
##detail.rb
belongs_to :user
##details controller
def new
@detail = Detail.new
user_id = session[:user_id]
company_id = params[:id]
end
def create
@detail = Detail.new(params[:detail])
@detail.user_id = session[:user_id]
@detail.company_id = params[:id]
end
###settings.html.erb
### this is where i make sure that company_id gets pass with the url
link_to 'New Detail', {:controller => 'details', :action =>'new', :id => company.id }, :class => 'plus'
#####routes
resources :details
resources :companies
resources :users
resources :sessions
I know this may not look pretty or proper if you know a better way please let me know...thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我立即注意到一些事情。您可能需要首先修复您的关联。假设一个用户有一个公司,一个公司有一个详细信息。
应该是:
虽然,取决于您的域要求。我通常将其设置为:User has_many Companies。由于详细信息与公司是 1:1,所以我会将所有详细信息包含在公司内部。
I notice something immediately. You may need to fix your associations first. Assuming, a user has one company and a company has one detail.
Should be:
Although, depending on your domain requirements. I would normally have it as: User has_many Companies. And since details is 1:1 with company, I would include all the details inside company.
直到后来我才意识到这一点,但我需要它将视图上的值传递给 from,就像这样
,我需要它在 from 上收集该值...就像这样
I didn't realize this until later but I need it pass values to from on the view to the from like so
and I need it to collect that value on the from...like so