开发者

How to pass object id?

I have a relationship between two models

Category Model

class Category < ActiveRecord::Base
   belongs_to :product
end

Product Model

class Product < ActiveRecord::Base
  has_many :categories
end

I have category_id in products table but When I c开发者_如何转开发reate 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.

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.

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

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.

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!


First off, you have the _id field in the wrong table. If Category belongs_to :product, then your categories table needs a field product_id.

Look at it this way: each Product can have many Categories - so what single value would you expect to find in a category_id field?

If you still have problems after correcting that, let me know.

Edit: Once you've got your tables set up, you still need to tell Rails what the link should be. You've got a few options. Assuming you've got a Category in hand, your best bet is new_prod = my_cat.create_product(). Alternatively, you can use new_prod = Product.create(:category => my_cat).

Later on, you can associate the models together like this:

my_prod.category = my_cat
my_prod.save
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜