J'ai reçu cette erreurModèle préféré #No route match {: action => "favori",: controller => "microposts",: id => nil,: type => "favori"} manquant clés requises: [: id]
ActionController :: UrlGenerationError à StaticPages # Accueil
Aucun résultat de route {: action => "favori",: controller => "microposts", : id => # < Micropost id: nul, content: nil, id_utilisateur: 101, created_at: nil, updated_at: nil, image: nil, texte: néant, type: néant, prix: néant>, : type => "favori"} manquant clés requises: [: id ]
Modèles:
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 Controller:
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
Routes:
Rails.application.routes.draw do
resources :microposts do
match :favorite, on: :member, via: [:put, :delete]
end
end
Voir:
<% 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%>
itinéraires de râteau
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
ceci est mon point de vue de la microtiges _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' %>
contrôleur Page statique
class StaticPagesController < ApplicationController
def home
if logged_in?
@micropost = current_user.microposts.build
@feed_items = Micropost.all.paginate(page: params[:page])
end
end
essayer 'favorite_micropost_path (@ micropost.id, tapez: « favori »)' – Vishal
Pouvez-vous poster la sortie de ' rails routes -c microposts'? – Vishal
Prefix Verbe URI motif de contrôleur # action favorite_micropost PUT | SUPPRIMER /microposts/:id/favorite(.:formAat) # microposts GET /microposts(.:format favoris) # microposts index POST /microposts(.:format) microposts # create –