[英]Ruby on Rails routes modification leads to error
試圖從修改我的routes.rb
resources :comments
resources :replies
至
resources :comments do
resources :replies
end
耙路呼叫后,路由分別從
Prefix Verb URI Pattern Controller#Action
replies GET /replies(.:format) replies#index
POST /replies(.:format) replies#create
new_reply GET /replies/new(.:format) replies#new
edit_reply GET /replies/:id/edit(.:format) replies#edit
reply GET /replies/:id(.:format) replies#show
PATCH /replies/:id(.:format) replies#update
PUT /replies/:id(.:format) replies#update
DELETE /replies/:id(.:format) replies#destroy
comments GET /comments(.:format) comments#index
至
Prefix Verb URI Pattern Prefix Verb URI Pattern Controller#Action
comment_replies GET /comments/:comment_id/replies(.:format) replies#index
POST /comments/:comment_id/replies(.:format) replies#create
new_comment_reply GET /comments/:comment_id/replies/new(.:format) replies#new
edit_comment_reply GET /comments/:comment_id/replies/:id/edit(.:format) replies#edit
comment_reply GET /comments/:comment_id/replies/:id(.:format) replies#show
PATCH /comments/:comment_id/replies/:id(.:format) replies#update
PUT /comments/:comment_id/replies/:id(.:format) replies#update
DELETE /comments/:comment_id/replies/:id(.:format) replies#destroy
結果,像edit_reply_path這樣的路徑不再起作用。
undefined method `edit_reply_path'
編輯:通過將其更改為comment_edit_reply_path來修復該路徑
replies / _form.erb中發生了新錯誤:
undefined method `replies_path' for #<#<Class:0x007ff3e8d9df08>:0x007ff3e0cd9458>
錯誤是針對以下行
<%= form_with(model: reply, local: true) do |form| %>
編輯:這是在控制器中創建
def create
@reply = @comment.replies.create(:user_id, :anonymous, :text, :post_id, :title).permit(:reply)
respond_to do |format|
if @reply.save
format.html { redirect_to @reply, notice: 'Reply was successfully created.' }
format.json { render :show, status: :created, location: @reply }
else
format.html { render :new }
format.json { render json: @reply.errors, status: :unprocessable_entity }
end
end
結束
當我嘗試創建新的comment.replies記錄並提交_form.html.erb...。我在控制台中收到以下錯誤:
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms)
ArgumentError (wrong number of arguments (given 5, expected 0..1)):
app/controllers/replies_controller.rb:29:in `create'
錯誤是關於這條線
@reply = @comment.replies.create(:user_id, :anonymous, :text, :post_id, :title).permit(:reply)
@約翰
因為您使用的是嵌套屬性,所以也必須包裝表單參數。 您可以同時嘗試1)使用form_with
<%= form_with(model: reply, url: comment_replies_path(@comment), local: true) do |form| %>
2)使用form_for
<%= form_for [:comment, @reply] do |form| %>
在這里@reply必須在新方法中聲明這樣的內容
@reply = Reply.new
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.