Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 不能';“找不到”;模型";没有身份证_Ruby On Rails 4_Params - Fatal编程技术网

Ruby on rails 4 不能';“找不到”;模型";没有身份证

Ruby on rails 4 不能';“找不到”;模型";没有身份证,ruby-on-rails-4,params,Ruby On Rails 4,Params,Rails无法在destroy方法中找到模型id,但可以在新方法中工作。不确定我是否遗漏了一些显而易见的东西 我正试图通过“取消订阅链接”销毁订阅实例。(订阅是用户和主题之间的联合表)。该视图位于主题显示页面上 我感到困惑的是,我能够在新方法中创建带有@theme=theme.find(params[:theme_id])的订阅实例,但在destroy方法中出现了错误。请参见下面的错误: ActiveRecord::在SubscriptionController中找不到RecordNot#销毁

Rails无法在destroy方法中找到模型id,但可以在新方法中工作。不确定我是否遗漏了一些显而易见的东西

我正试图通过“取消订阅链接”销毁订阅实例。(订阅是用户和主题之间的联合表)。该视图位于主题显示页面上

我感到困惑的是,我能够在新方法中创建带有@theme=theme.find(params[:theme_id])的订阅实例,但在destroy方法中出现了错误。请参见下面的错误:

ActiveRecord::在SubscriptionController中找不到RecordNot#销毁 找不到没有ID的主题

提取的源(第19行附近):

订阅控制器:

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
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
和视图为“取消订阅链接”传递不同的参数:

引用,哟。
当前_user.id,:theme_id=>@theme.id,数据:{confirm:'您确定要订阅吗?'})%> 当前_user.id,:theme_id=>@theme.id),:method=>:delete,数据:{confirm:'确定要取消订阅吗?'}%>


我对我如何调用@subscription.destroy_all并不感到太高兴,因为用户和主题之间实际上应该只有一个订阅实例,但它现在正在工作。

看到这一行了吗

 <%= button_to "UnSubscribe!", subscription_path(@current_sub), :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>
:删除,数据:{确认:'确实要取消订阅吗?}%>
您没有提到主题或主题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):方法=>:删除,数据:{confirm:'确定要取消订阅吗?'}%>

这将传回主题id,因此现在您可以在控制器中正确使用它。

谢谢您的回答!但我似乎还是犯了同样的错误。我为什么不需要:theme_id=>@theme.id)在新方法中,但在destroy方法中需要它?不需要什么?你能打印出你的控制台给你的确切输出吗?
subscription\u path(@current\u sub),:theme\u id=>@theme.id
有效吗?基本上它没有传递这个theme\u id,这可能是因为它不在路径中,但这有点奇怪
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
<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 %>
 <%= button_to "UnSubscribe!", subscription_path(@current_sub), :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>
<%= button_to "UnSubscribe!", subscription_path(@current_sub, :theme_id => @theme.id) :method => :delete, data: { confirm: 'Are you sure you want to unsubscribe?' } %>