Ruby on rails 收藏夹模型#无路由匹配{:action=>;favorite";,:controller=>;microposts";,:id=>;nil,:type=>;favorite";}缺少必需的键:[:id]
我犯了这个错误 ActionController::静态页面中的UrlGenerationError#主页Ruby on rails 收藏夹模型#无路由匹配{:action=>;favorite";,:controller=>;microposts";,:id=>;nil,:type=>;favorite";}缺少必需的键:[:id],ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Ruby On Rails 5,我犯了这个错误 ActionController::静态页面中的UrlGenerationError#主页 没有路由匹配{:action=>“favorite”,:controller=>“microposts”, :id=>#, :type=>“favorite”}缺少必需的键:[:id] 型号: class User< ApplicationRecord has_many :microposts,dependent: :destroy has_many :favorite_
没有路由匹配{:action=>“favorite”,:controller=>“microposts”, :id=>#, :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
视图:
这是我对微观经济学的看法
_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>
ay 7aga
EGP
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
class StaticPagesController
ActionController::静态页面中的UrlGenerationError#主页无路由
匹配{:action=>“favorite”,:controller=>“microposts”,
:id=>#,
:type=>“favorite”}缺少必需的键:[:id]
问题在于@micropost=current_user.micropost.build
,它将是一条未保存的记录。这意味着@micropost
的:id
将为nil,因此出现错误
您需要为一个用户获取所有的microspots,并像这样对其进行迭代
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 %>
def home
如果你登录了?
@microposts=当前用户。microposts
@feed_items=Micropost.all.paginate(第页:参数[:第页])
结束
结束
ActionController::静态页面中的UrlGenerationError#主页无路由
匹配{:action=>“favorite”,:controller=>“microposts”,
:id=>#,
:type=>“favorite”}缺少必需的键:[:id]
问题在于@micropost=current_user.micropost.build
,它将是一条未保存的记录。这意味着@micropost
的:id
将为nil,因此出现错误
您需要为一个用户获取所有的microspots,并像这样对其进行迭代
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 %>
def home
如果你登录了?
@microposts=当前用户。microposts
@feed_items=Micropost.all.paginate(第页:参数[:第页])
结束
结束
试试这个收藏夹的microspost\u路径(@microspost.id,键入:“favorite”)
你能发布rails routes-c microspost的输出吗?前缀动词URI模式控制器#Action favorite\u microspost PUT | DELETE/microspost/:id/favorite(:format)MicroPost#favorite GET/MicroPost(:format)MicroPost#index POST/MicroPost(:format)MicroPost#Create为什么将其标记为rails 3、rails 4和rails 5?是哪一个?(我猜是5,因为您使用的是ApplicationRecord
)请删除其他标记。@KhaledAbdEl Moneim发布问题中的路线详细信息尝试此favorite\u micropost\u路径(@microspost.id,键入:“favorite”)
你能发布rails routes-c microposts的输出吗?
?前缀动词URI模式控制器#动作收藏夹"micropost PUT |删除/microposts/:id/favorite(:format)microposts#收藏夹GET/microposts(:格式)microPost#index POST/microPost(:format)microPost#created为什么将其标记为rails 3、rails 4和rails 5?是哪一个?(我猜是5,因为您正在使用ApplicationRecord
)请删除其他标记。@KhaledAbdEl Moneim在questionActionView::Template::Error中发布路线详细信息(未定义的方法'each'表示nil:NilClass):它给了我那个errorActionView::Template::Error(未定义的方法'each'表示nil:NilClass):它给了我那个错误
.....
<% 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
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 %>