Nie wiem, jak ustawić więcej niż 1 element "metody zasobów. Mam 3 rusztowania, czyli post, komentarz, odpowiedź. Chcę to osiągnąć na trasach rake: "new_post_comment_response => / posts / id / comment / id / response / new"
Relacje są następujące:
"Model postu ma wiele: komentarze ma wiele: odpowiedzi
Model komentarza ma wiele: odpowiedzi belongs_to: post
Model odpowiedzi belongs_to: post belongs_to: comment "
W routes.rb ustawiłem:
zasoby: posty do zasoby: komentarze, z wyjątkiem: [: pokaż,: indeks] koniec
zasoby: komentarze do zasoby: odpowiedzi, z wyjątkiem: [: pokaż,: indeks] koniec
Ale chcę mieć trzy pozycje na trasach rake'u, ponieważ zamierzam zrobić coś "trzy odpowiedzi na forach (komentarz po komentarzach do komentarza)." Czy wiesz, co mam na myśli?
Moje pytanie brzmi: jak skonfigurować zasoby i kontrolery, aby wykryć identyfikator posta i komentarza oraz jak skonfigurować zasoby.
Dzięki za pomoc!
Odpowiedzi:
1 dla odpowiedzi № 1Po prostu musisz zagnieździć zasoby wtak jak chcesz. Oznacza to, że posty powinny mieć zagnieżdżone komentarze i odpowiedzi, a komentarze powinny mieć zagnieżdżone odpowiedzi. Prawdopodobnie możesz ustawić coś podobnego do tego, zakładając, że poprawnie odczytałem twoje wymagania.
concern :responsable do
resources :responses, except: [:show, :index]
end
resources :posts, concerns: :responsable do
resources :comments, except: [:show, :index], concerns: :responsable
end
Obawy są po prostu sposobem na ponowne użycie zestawu wspólnych tras. Więcej o tym jest w dokumentacja.
Jeśli uruchomisz trasy rake, powinieneś skończyć z następującymi ścieżkami:
post_comment_responses POST /posts/:post_id/comments/:comment_id/responses(.:format) responses#create
new_post_comment_response GET /posts/:post_id/comments/:comment_id/responses/new(.:format) responses#new
edit_post_comment_response GET /posts/:post_id/comments/:comment_id/responses/:id/edit(.:format) responses#edit
post_comment_response PATCH /posts/:post_id/comments/:comment_id/responses/:id(.:format) responses#update
PUT /posts/:post_id/comments/:comment_id/responses/:id(.:format) responses#update
DELETE /posts/:post_id/comments/:comment_id/responses/:id(.:format) responses#destroy
post_comments POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
post_responses POST /posts/:post_id/responses(.:format) responses#create
new_post_response GET /posts/:post_id/responses/new(.:format) responses#new
edit_post_response GET /posts/:post_id/responses/:id/edit(.:format) responses#edit
post_response PATCH /posts/:post_id/responses/:id(.:format) responses#update
PUT /posts/:post_id/responses/:id(.:format) responses#update
DELETE /posts/:post_id/responses/:id(.:format) responses#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy