【问题标题】:devise creates malfunction in my rails app设计在我的 Rails 应用程序中造成故障
【发布时间】:2012-07-17 13:24:31
【问题描述】:

我在这里发布的设计已经存在问题:Weird route malfunction, mix between devise and own controller
我希望这只是一次性问题,但设计让我发疯了!

如果我这样做,一切都好

<% if user_signed_in? %>
  <% if current_user.ideas.include?(@idea) %>
    <%= button_to 'Remove from Favorites', user_idea_subscription(@idea.id), method:    :delete%>
  <% else %>
    <%= button_to 'Add to Favorites', user_idea_subscriptions_path(current_user, :idea_id => @idea) %>
  <% end %>
<% end %>

但如果我这样做:

<% if user_signed_in? and current_user.ideas.include?(@idea) %>
  <%= button_to 'Remove from Favorites', user_idea_subscription(@idea.id), method:    :delete%>
<% else %>
  <%= button_to 'Add to Favorites', user_idea_subscriptions_path(current_user, :idea_id => @idea) %>
<% end %>

然后我得到了这个错误:

No route matches {:controller=>"idea_subscriptions", :idea_id=>#<Idea id: 12, name: "Chute libre", description: "50sec a 50m/s", created_at: "2012-07-12 10:02:30", updated_at: "2012-07-12 10:02:30">}

一切都应该没问题,但为什么我会收到这个错误? (除了错误没有其他信息)
感谢您的帮助和我的理智!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise routes


    【解决方案1】:

    我认为这是正常行为,因为在第二种情况下,您正在尝试调用

    user_idea_subscriptions_path(current_user, :idea_id => @idea)
    

    那么用户没有登录,所以current_user 为nil

    【讨论】:

    • 哦,好的,谢谢!我这样做是因为即使用户未登录,我也想让他看到添加收藏夹按钮,但在控制器中我会设置一个 Flash 通知“注册以将想法添加为收藏”并重定向到 @idea。那我该怎么做呢?
    • 你不能在没有 user_id 的情况下使用这个路径助手,所以你必须为未经授权的用户使用另一个路径,或者只使用一些带有“#not_authorized”url的javascript。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多