I Have a scenario, which I am not able to figure out,
我有一个情形,我无法算出来,
Profile Product
customer1 Iphone
customer2 Iphone
Now customer1 sells his iphone, so i will have to delete the association between customer1 and iphone but not the profile or product. I have an intermediate table for many to many relationship called product_profiles
with product_id
and profile_id
as fields. Hence i need to delete id's from product_profiles table.
现在customer1出售了他的iphone,所以我必须删除customer1和iphone之间的关联,而不是配置文件或产品。我有一个中间表,其中许多关系称为product_profiles,它的product_id和profile_id作为字段。因此,我需要从product_profiles表删除id。
Product model
产品模型
has_many :product_profiles
has_many :profiles, :through => :product_profiles
Profile model
剖面模型
has_many :product_profiles
has_many :products, :through => :product_profiles
Product controller
产品控制器
def destroy
if ProductProfile.where(:product_id => params[:id]).where(:profile_id => ?).destroy //how to get the profile_id from view when clicked on delete button
redirect_to admin_products_path
flash[:success] = "Product Successfully Deleted!"
else
flash[:error] = "Sorry! Could not complete the request, please try again!"
redirect_to :action => 'index'
end
end
View file
视图文件
<% @products.each do |b| %>
<tr>
<td><%= @profile.name %></td>
<td>
<% b.sub_categories.each do |p| %>
<%= p.name + ',' %>
<% end %>
</td>
<td><%= b.name %></td>
<td>
<%= link_to "<span class='glyphicon glyphicon-edit'></span>".html_safe, edit_admin_product_path(b) %>
</td>
<td>
<%= link_to "<span class='glyphicon glyphicon-trash'></span>".html_safe, admin_product_path(b), :method => :delete, :title => "Delete Brand", "data-confirm" => "Do you really want to delete?" %>
</td>
</tr>
<% end %>
2 个解决方案
#1
1
You can pass parameters using the second argument of the path helper.
可以使用路径助手的第二个参数传递参数。
<%= link_to "<span class='glyphicon glyphicon-trash'></span>".html_safe, admin_product_path(b, :profile_id => @profile.id), :method => :delete, :title => "Delete Brand", "data-confirm" => "Do you really want to delete?" %>
In your controller you would then be able to access params[:profile_id]
在您的控制器中,您将能够访问params[:profile_id]
#2
1
product = Product.find_by_id(product_id)
product.product_profiles.delete(product_profile_id)
Delete is not going to destroy your objects only the association between them.
删除不会破坏你的对象,只会破坏它们之间的关联。
Source: http://neyric.com/2007/07/08/how-to-delete-a-many-to-many-association-with-rails/
来源:http://neyric.com/2007/07/08/how-to-delete-a-many-to-many-association-with-rails/
#1
1
You can pass parameters using the second argument of the path helper.
可以使用路径助手的第二个参数传递参数。
<%= link_to "<span class='glyphicon glyphicon-trash'></span>".html_safe, admin_product_path(b, :profile_id => @profile.id), :method => :delete, :title => "Delete Brand", "data-confirm" => "Do you really want to delete?" %>
In your controller you would then be able to access params[:profile_id]
在您的控制器中,您将能够访问params[:profile_id]
#2
1
product = Product.find_by_id(product_id)
product.product_profiles.delete(product_profile_id)
Delete is not going to destroy your objects only the association between them.
删除不会破坏你的对象,只会破坏它们之间的关联。
Source: http://neyric.com/2007/07/08/how-to-delete-a-many-to-many-association-with-rails/
来源:http://neyric.com/2007/07/08/how-to-delete-a-many-to-many-association-with-rails/