Rails3 没有将第二个 id 添加到我的表中

发布于 2024-11-15 23:13:49 字数 924 浏览 1 评论 0原文

我遇到的问题是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-11-22 23:13:49

我立即注意到一些事情。您可能需要首先修复您的关联。假设一个用户有一个公司,一个公司有一个详细信息。

##user.rb
has_one :company

##company.rb
belongs_to :user
has_one :detail

##detail.rb
belongs_to :user

应该是:

##user.rb
has_one :company

##company.rb
belongs_to :user
has_one :detail

##detail.rb
belongs_to :company

虽然,取决于您的域要求。我通常将其设置为: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.

##user.rb
has_one :company

##company.rb
belongs_to :user
has_one :detail

##detail.rb
belongs_to :user

Should be:

##user.rb
has_one :company

##company.rb
belongs_to :user
has_one :detail

##detail.rb
belongs_to :company

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.

帅哥哥的热头脑 2024-11-22 23:13:49

直到后来我才意识到这一点,但我需要它将视图上的值传递给 from,就像这样

  <% @companies.each do |company| %>
      <%= link_to 'New Detail', {:controller =>'details', :action => 'new', :company_id => company.id},  :class => 'plus' %>
    <% end %>

,我需要它在 from 上收集该值...就像这样

<%= form_for(:detail, :url =>{:action => 'create', :company_id => @company.id}) do |f| %>
...(values)
<% end %>

I didn't realize this until later but I need it pass values to from on the view to the from like so

  <% @companies.each do |company| %>
      <%= link_to 'New Detail', {:controller =>'details', :action => 'new', :company_id => company.id},  :class => 'plus' %>
    <% end %>

and I need it to collect that value on the from...like so

<%= form_for(:detail, :url =>{:action => 'create', :company_id => @company.id}) do |f| %>
...(values)
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文