简体   繁体   中英

No Method Error - Devise

I'm currently receiving this error when trying to do this route.

undefined method `user_tourcomplete_offer_path'

I don't know how to fix it, tried about everything.

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

rake routes

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

offers#tourcomplete

Full Error

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]

undefined method `user_tourcomplete_offer_path'

As per your routes , it should be user_offer_tourcomplete_path .

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

You are only passing referance of user with path.

user_offer_tourcomplete_path(current_user)

But the actual route generated will be:

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

So offer referance is also necesssary. You also need to pass offer_id in path.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM