【问题标题】:No Method Error - Devise没有方法错误 - 设计
【发布时间】:2015-10-17 18:21:14
【问题描述】:

我目前在尝试执行此路线时收到此错误。

未定义的方法`user_tourcomplete_offer_path'

我不知道如何解决它,尝试了所有方法。

routes.rb

  devise_for :users
  resources :offers

  # Root
  root 'welcome#index'

  # Rails Admin Engine
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'

  # User ID in URL
  resources :users, path:"u" do
   resources :offers do #-> url.com/users/:user_id/offers
      put :tourcomplete #-> you don't need member do
   end
   resources :categories, only: :show
  end

index.html.erb

<% if current_user.tourcomplete == true %>

<% else %>

<ol id="tour">

  <li class="welcome-tour">
    <%= image_tag "welcome-smiley.png" %>
    <h2>Welcome to Bundel</h2>
    <p>Pick from over 3,000 offers available at Bundel, all you have to do is click and shop just like usual.</p>
    <a href="#" class="joyride-next-tip">Start Tour</a>
  </li>

  <li data-class="offer">
    <h4>Pick your Offer</h4>
    <p>Pick from over 3,000 offers available at Bundel, all you have to do is click and shop just like usual.</p>
    <a href="#" class="joyride-next-tip">Next</a>
  </li>

  <li data-class="balance">
    <h4>Your Balance</h4>
    <p>How much you've earnt via Bundel, this will increase on purchasing.</p>
    <a href="#" class="joyride-next-tip">Next</a>
  </li>

  <li data-class="values">
    <h4>Cashback Values</h4>
    <p>This is how much cash back you will receive on any purchase made with this retailer. (Please read the terms on hover)</p>
    <%= link_to "Finish", user_offer_tourcomplete_path(current_user), class: "joyride-next-tip", method: :put %>
  </li>
</ol>

<script>
$(window).load(function() {
  $('#tour').joyride({
    autoStart : true,
    tipLocation: 'right',         // 'top' or 'bottom' in relation to parent
    nubPosition: 'auto',           // override on a per tooltip bases
    scrollSpeed: 300,              // Page scrolling speed in ms
    nextButton: false,              // true/false for next button visibility
    tipAnimation: 'fade',           // 'pop' or 'fade' in each tip
  });
});
</script>

<% end %>

offers_controller.rb

  def index
    @offers = Offer.all

    def tourcomplete
      current_user.update_attributes(tourcomplete: true)
      redirect_to root_url
    end

    @featured_offers = Offer.where(featured: true)

  end

耙路

 user_offer_tourcomplete PUT    /u/:user_id/offers/:offer_id/tourcomplete(.:format)

提供#tourcomplete

完全错误

No route matches {:action=>"tourcomplete", :controller=>"offers", :offer_id=>nil, :user_id=>#<User id: 2, email: "test@test.com", encrypted_password: "$2a$10$RSX5sD9G5ipdtDAG32BirOw3WDma13yuHYV6bNQTb5Y...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-10-17 18:16:55", last_sign_in_at: "2015-10-17 18:16:55", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-10-17 18:16:55", updated_at: "2015-10-17 18:16:55", beta_key: "ahfyqom124k19jsalx9", paypal: nil, balance: 0.0, admin: false, tourcomplete: false>} missing required keys: [:offer_id]

【问题讨论】:

  • Pavan 的回答是正确的。作为提示,rake 路线是您的朋友!只需在命令行中输入rake routesbundle exec rake routes。您将看到所有当前路由,第一列将是路径辅助方法(例如 user_offer_tourcomplete)。

标签: ruby-on-rails ruby devise routes


【解决方案1】:

您只是通过路径传递用户的引用。

user_offer_tourcomplete_path(current_user)

但生成的实际路线将是:

user_offer_tourcomplete PUT    /u/:user_id/offers/:offer_id/tourcomplete(.:format)

所以提供参考也是必要的。您还需要在路径中传递 offer_id。

<% offer = some_offer_object %>     
<%= link_to "Finish", user_offer_tourcomplete_path(current_user,offer), class: "joyride-next-tip", method: :put %>

【讨论】:

  • 你能在link_to函数中尝试一下user_offer_tourcomplete_path(current_user,1)并检查一下
  • @Jonathan 我提到的上述建议有什么运气吗?
  • 你的问题和 pavans 的问题混在一起
  • 由于某种原因它与 current_user,1 一起工作。请更改您的答案以帮助他人。这是 pavans 路径加上你的评论。
  • 以前的路径只是一个复制错误。我已经编辑了答案。
【解决方案2】:

未定义的方法`user_tourcomplete_offer_path'

根据您的routes,应该是user_offer_tourcomplete_path

<%= link_to "Finish", user_offer_tourcomplete_path(current_user), class: "joyride-next-tip", method: :put %>

【讨论】:

  • 未定义的方法 `user_offer_tourcomplete' 仍然得到它。我将在问题中发布 rake 路线和我的控制器
  • @Jonathan 也发布你现在得到的完整错误。
  • 我现在更新了提供 rake 路由和控制器的问题。感谢您的帮助:)
  • @Jonathan 您能否按照我的建议发布您在更改后得到的完整错误?
  • 道歉链接错误 - 请立即查看问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2018-04-05
  • 2023-03-29
相关资源
最近更新 更多