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):它给我那个错误 –

相关问题