1

我得到這個錯誤Favorite Model#無路線匹配{:action =>「favorite」,:controller =>「microposts」,:id => nil,:type =>「favorite」}缺少必需的鍵:[:id]

的ActionController :: UrlGenerationError在StaticPages#家
沒有路由匹配{:動作=> 「最愛」,:控制器=> 「微柱」, :ID =>#< ID微柱:無, content:nil,user_id:101,created_at: nil,updated_at:nil,picture:nil,text:nil,type:nil,price:nil, :type =>「favorite」}缺少必需的鍵:[:id ]

型號:

class User< ApplicationRecord 
    has_many :microposts,dependent: :destroy 
    has_many :favorite_microposts 
    has_many :favorites, through: :favorite_micoposts, source: :micropost  
end 

class Micropost < ApplicationRecord 
    has_many :favorite_microposts 
    has_many :favorited_by, through: :favorite_microposts, source: :user 
end   

class FavoriteMicropost < ApplicationRecord 
    belongs_to :user 
    belond_to :micropost 
end 

微柱控制器:

class MicropostsController < ApplicationController 
... 
def favorite 
@micropost = Micropost.find(params[:id]) 
type = params[:type] 
if type == "favorite" 
    current_user.favorites << @micropost 
    redirect_to :back, notice: 'You favorited #{@micropost.number}' 

elsif type == "unfavorite" 
    current_user.favorites.delete(@micropost) 
    redirect_to :back, notice: 'Unfavorited #{@micrpost.number}' 

else 
    # Type missing, nothing happens 
    redirect_to :back, notice: 'Nothing happened.' 
    end 
    end 
end 

途徑:

Rails.application.routes.draw do 
resources :microposts do 
    match :favorite, on: :member, via: [:put, :delete] 
    end 
end 

檢視:

<% if current_user %> 
<%= link_to "favorite", favorite_micropost_path(@micropost, type:"favorite"), method: :put%> 
<%= link_to "unfavorite", favorite_micropost_path(@micropost, type: "unfavorite"), method: :put %> 
<%end%> 

耙路由

Prefix Verb  URI Pattern      Controller#Action 
    microposts POST  /microposts(.:format)    microposts#create 
    micropost DELETE  /microposts/:id(.:format) microposts#destroy 
favorite_micropost PUT|DELETE /microposts/:id/favorite(.:format) microposts#favorite 
       GET  /microposts(.:format)    microposts#index 
       POST  /microposts(.:format)    microposts#create 
new_micropost GET  /microposts/new(.:format)   microposts#new 
edit_micropost GET  /microposts/:id/edit(.:format) microposts#edit 

       GET  /microposts/:id(.:format)   microposts#show 
       PATCH  /microposts/:id(.:format) microposts#update 
       PUT  /microposts/:id(.:format) microposts#update 
       DELETE  /microposts/:id(.:format) microposts#destroy 

這是我的微柱 _micropost.html.erb的觀點:

<div class="item col-xs-4 col-lg-4"> 
     <div class="thumbnail" > 
      <div class=img3> 
      <%= image_tag micropost.picture.url ,class:"img3" %> 
      </div> 
      <div class="caption"> 
       <h4 >ay 7aga</h4> 
       <p class="group inner list-group-item-text"> 
        <%= micropost.content %></p> 
       <div class="row"> 
        <div class="col-xs-12 col-md-6"> 
         <p class="lead"> 
          <%= micropost.price %> 
          EGP</p> 
        </div> 
        <div class="col-xs-12 col-md-6"> 

        <% if current_user %> 
        <%= link_to "favorite", favorite_micropost_path(@micropost.id, type: "favorite"), method: :put %> 
        <%= link_to "unfavorite", favorite_micropost_path(@micropost.id, type: "unfavorite"), method: :put %> 

         <%end%> 
        </div> 
       </div> 
      </div> 
     </div> 
</div> 

home.html.erb

..... 

<% if logged_in? %> 


    <div class="col-md-8"> 
<div class="container"> 
    <strong>feeds</strong> 
    <%= render 'shared/feed' %> 

靜態頁面控制器

class StaticPagesController < ApplicationController 
    def home 
    if logged_in? 
     @micropost = current_user.microposts.build 
     @feed_items = Micropost.all.paginate(page: params[:page]) 
    end 
    end 
+0

試試這個'favorite_micropost_path(@ micropost.id,鍵入:「最愛」)' – Vishal

+0

你能後的'輸出鐵路路線-c microposts'? – Vishal

+0

前綴動詞URI模式控制器#行動 favorite_micropost PUT | DELETE /microposts/:id/favorite(.:formAat)微柱#喜愛 GET /microposts(.:format)微柱#指數 POST /microposts(.:format) microposts#create –

回答

0

StaticController中的ActionController :: UrlGenerationError#home沒有路由 匹配{:action =>「favorite」,:controller =>「microposts」, :id =>#, :type =>「favorite」}缺少必需的密鑰:[:ID]

問題是與@micropost = current_user.microposts.build這將是一個未保存的記錄。這意味着:id對於@micropost將是,因此錯誤。

你需要獲取所有微觀柱爲用戶和遍歷它像這樣

def home 
    if logged_in? 
    @microposts = current_user.microposts 
    @feed_items = Micropost.all.paginate(page: params[:page]) 
    end 
end 

<% if current_user %> 
    <% @microposts.each do |micropost| %> 
    <%= link_to "favorite", favorite_micropost_path(micropost, type: "favorite"), method: :put %> 
    <%= link_to "unfavorite", favorite_micropost_path(micropost, type: "unfavorite"), method: :put %> 
    <% end %> 
<% end %> 
+0

ActionView :: Template :: Error(未定義的方法'each'for nil:NilClass):它給我那個錯誤 –

相關問題