如何传递对象ID? [英] How to pass object id?

查看:37
本文介绍了如何传递对象ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型之间的关系

I have a relationship between two models

类别模型

class Category < ActiveRecord::Base
   belongs_to :product
end

产品型号

class Product < ActiveRecord::Base
  has_many :categories
end

我在产品表中有 category_id 但是当我在我的产品表中创建新产品时category_id 为空.我是 Rails 新手,有人可以帮忙吗?

I have category_id in products table but When I create a new product in my products table category_id is null. I am new to rails can anyone help please?

推荐答案

首先,一个想法——大多数时候,产品有很多类别,但每个类别也包含很多产品.也许您的关联应该是多对多的?关于您的实际问题.

First off, a thought - most of the time, products have many categories, but each category also contains many products. Perhaps your association should be a many-to-many? On to your actual question.

如果我理解正确的话,您的问题实际上是关于如何在数据库中创建相互关联的类别和产品,即如何在构建新类别时设置product_id 的值.

If I understand correctly, your question is really about how to create categories and products that are related to each other in the database, i.e. how to set the value of product_id when building a new category.

为清楚起见,以防您需要它,product_id 只会为类别设置.毕竟,类别属于该产品,因此它必须持有其所有者的 ID.

For clarity in case you need it, product_id would only be set for a category. The category, after all, belongs to that product, so it has to hold its owner's ID.

因此,假设您想构建属于现有产品的新类别 - 您可以这样做:

So, let's say you want to build a new category that belongs to an existing product - you can do this:

# in your view, where you link from products/show.html.erb to category/new.html.erb
<%= link_to "Build new category for this product", new_category_url(:id => @product.id) %>
    # you must have @product defined, and you also must have 
    # 'resources :categories' in your routes file

# in your controller action categories/new, set the new category's product id:
def new
  @category = Category.new(:product_id => params[:id])
end

# include a hidden field to contain the product_id in your new form
<%= form_for @category do |f| %>
  <%= f.hidden_field :product_id %>
  ... other fields, labels, etc.
<% end %>

# save the record as you normally would (analogous to the code in your comment to @Chowlett).
@category = Category.new(params[:category])
if @category.save
  redirect_to :action => "list", :notice => "Category saved successfully."
else
  render :action => "new"
end

上面的代码可以让你构建一个产品,然后每个类别一个一个.因此,我们首先构建您的产品,然后包含从产品/展示页面到您的类别/新表单的链接,并传入您希望该类别所属的产品的 ID.

The above code allows you to build a product, then each category one-by-one. So, we are building your product first, then including a link from the product/show page to your category/new form, passing in the ID of the product you want that category to be part of.

如果你想同时构建一个产品和一些类别,那就有点复杂了.有关这方面的更多信息,请查看 http://railscasts.com/episodes/196-nested-model-form-part-1(这是三部分系列的第一部分)和 https://github.com/ryanb/nested_form.除非您对上述基础知识非常满意,否则我不建议您执行此操作.当我刚接触 Rails 时,我曾经被这段代码困了一个星期!

If you want to build a product and some categories at the same time, it is a bit more complicated. For more information on this, take a look at http://railscasts.com/episodes/196-nested-model-form-part-1 (this is the first of a three-part series) and https://github.com/ryanb/nested_form. I don't suggest this course of action unless you are very comfortable with the above basics. I once got mired in this code for a week when I was new to Rails!

这篇关于如何传递对象ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆