【发布时间】:2016-03-03 03:16:19
【问题描述】:
我的项目是关于一个在线购物网站,使用 Ruby on Rails 购买手机。
我的网站有一个添加手机的页面 - 三星、诺基亚……在三星,它有很多设备。 如何获取三星的 ID 以创建“三星”类型的新手机。三星在产品表中,手机在电话表中。
class Phone < ActiveRecord::Base
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :phones
end
这是产品的动作表演:
<h1>Your item</h1>
<h3><%= @product.name %></h4>
<% if logged_in?%>
<% if current_user.admin? %>
<%= link_to 'Edit',edit_product_path%>
<%end%>
<%end%>
<%= link_to 'Home',welcome_home_path%>
<%= link_to 'New item',new_phone_path %>
<%= link_to 'Create new phone',new_phone_path%> #It links to action new of Phones
但我无法获取 Product 的 ID 来做:`object_product.phones.create
class PhoneController < ApplicationController
def new
end
def show
@phone = Phone.find(params[:phone_id])
end
def create
@product = Product.find(params[:product_id])
@phone = @product.phones.create(phone)
redirect_to product_phone_path
end
private
def phone
params.require(:phone).permit(:name,:num)
end
end
【问题讨论】:
-
Hung,请检查我的编辑,看看是否是您所要求的。
标签: ruby-on-rails ruby