Rails在destroy方法中找不到Model ID但在新方法中有效。不确定我是否遗漏了一些明显的东西。
我试图通过“取消订阅链接”销毁订阅的实例。 (订阅是用户和主题之间的联合表)。该视图位于主题展示页面上。
我感到困惑的是,我能够在新方法中使用@theme = Theme.find(params [:theme_id])创建订阅实例,但错误会在destroy方法中出现。请参阅以下错误:
SubscriptionsController#destroy中的提取的来源(第19行):
def destroy
@theme = Theme.find(params[:theme_id]) #line 19
@current_sub = Subscription.where(:user_id => current_user.id, :theme_id => @theme.id)
@current_sub.destroy
flash[:notice] = "You have unsubscribed to #{@theme.name}"
订阅控制器:
class SubscriptionsController < ApplicationController
def new
@theme = Theme.find(params[:theme_id])
@current_sub = Subscription.where(:user_id => current_user.id, :theme_id => @theme.id)
while @current_sub.count == 0 do
@subscription = Subscription.new
@subscription.user_id = current_user.id
@subscription.theme_id = @theme.id
if @subscription.save
flash[:notice] = "You have subscribed to #{@theme.name}"
redirect_to root_path
else
render new
end
end
end
def destroy
@theme = Theme.find(params[:theme_id])
@current_sub = Subscription.where(:user_id => current_user.id, :theme_id => @theme.id)
@current_sub.destroy
flash[:notice] = "You have unsubscribed to #{@theme.name}"
redirect_to root_path
end
end
主题显示视图:
<h1>QUOTES, YO.</h1>
<br>
<h3>
<% if @current_sub.count == 0 %>
<%= link_to "Subscribe!", new_subscription_path(:user_id => current_user.id, :theme_id => @theme.id, data: { confirm: 'Are you sure you want to subscribe?' } )%>
<% else %>
<%= button_to "UnSubscribe!", subscription_path(@current_sub), :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>
<% end %>
</h3>
<br>
<% @theme.inspirations.each do |i| %>
<%= i.quote %>
<br>
<% end %>
<br>
<%= link_to "Back to Main", root_path %>
主题控制器:
class ThemesController < ApplicationController
def index
@themes = Theme.all
end
def show
@theme = Theme.find(params[:id])
@current_sub = Subscription.where(:user_id => current_user.id, :theme_id => @theme.id)
end
private
def theme_params
params.require(:theme).permit(:name)
end
end
更新 这可能不是最佳选择,但我通过将控制器更改为:
来修复我的代码def destroy
@theme = Theme.find(params[:theme_id])
@subscription = Subscription.where(:user_id => current_user.id, :theme_id => @theme.id)
@subscription.destroy_all
flash[:notice] = "You have unsubscribed to #{@theme.name}"
redirect_to root_path
end
并查看传递“Unsubscribe Link”的不同参数:
<h1>QUOTES, YO.</h1>
<br>
<h3>
<% if @current_subs.count == 0 %>
<%= link_to "Subscribe!", new_subscription_path(:user_id => current_user.id, :theme_id => @theme.id, data: { confirm: 'Are you sure you want to subscribe?' } )%>
<% else %>
<%= button_to "UnSubscribe!", subscription_path(:user_id => current_user.id, :theme_id => @theme.id), :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>
<% end %>
</h3>
<br>
<% @theme.inspirations.each do |i| %>
<%= i.quote %>
<br>
<% end %>
<br>
<%= link_to "Back to Main", root_path %>
我不会对我如何调用@ subscription.destroy_all感到欣喜若狂,因为在用户和主题之间确实应该只有一个订阅实例,但它现在正在工作。
答案 0 :(得分:0)
看到这一行?
<%= button_to "UnSubscribe!", subscription_path(@current_sub), :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>
您没有提及主题或theme_id,因此您需要这样做:
<%= button_to "UnSubscribe!", subscription_path(@current_sub, :theme_id => @theme.id) :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>
这将传回theme_id,所以现在你可以在控制器中正确使用它了。