我正在为一个大型移动应用程序开发一个RoR后端,目前正在使用管理面板。
我有两个模型:Activity
和Deal
,由HMT ActivitiesDeal
加入。连接在rails控制台中以两种方式进行测试,并且像魅力一样。
活动是构建应用程序的模型,因此管理员需要能够以某种直观的方式从“编辑活动”表单向活动添加交易。 我试过这个来创建activities_deal:
<%=select("deal", @deal_id, Deal.all.collect {|d| [d.title, d.id]}, {})%>
<%= link_to "Add", link_activity_deal_path(activity_id: @activity.id, deal_id: @deal_id), method:'post' %>
但它并没有像我想的那样奏效。有关如何将正确的deal_id发送到link_activity_deal_path的任何想法?这似乎是一个已经解决过很多次的问题,但我可以找到适合的任何东西。
ActivitiesDealsController:
class ActivitiesDealsController < ApplicationController
def create
@activity = Activity.find(params[:activity_id])
render file: 'public/404.html' and return unless @activity && Deal.find(params[:deal_id])
@activity_deal = ActivitiesDeal.new
@activity_deal.activity_id = params[:activity_id]
@activity_deal.deal_id = params[:deal_id]
if @activity_deal.save
redirect_to proc {activity_url @activity}
end
render file: 'public/500.html'
end
def destroy
p params
@activity = Activity.find(params[:activity_id])
render file: 'public/404.html' and return unless @activity
@activity_deal = ActivitiesDeal.where("activity_id == ? AND deal_id == ?", params[:activity_id], params[:deal_id])
render file: 'public/404.html' and return unless @activity_deal
ActivitiesDeal.destroy(@activity_deal)
redirect_to proc {activity_url @activity}
end
end
答案 0 :(得分:0)
通过在编辑页面之外创建form_for来解决问题。
如果有人需要代码:
<%= form_for @activity, as: :deal, :url => link_activity_deal_path(activity_id: @activity.id), method:'post' do |f|%>
<%= f.collection_select :id, @deals, :id, :title %>
<%= f.submit "Add Deal", class: "btn btn-primary" %>