Ruby on Rails路由修改导致错误

时间:2017-11-25 06:21:06

标签: ruby-on-rails database routes

尝试从

修改我的routes.rb
resources :comments
resources :replies

resources :comments do
resources :replies
end

在rake路由调用后,路由分别从

更改
   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

来修复路径

在回复/ _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)

2 个答案:

答案 0 :(得分:2)

我认为您尝试将嵌套路由添加为link

所以你的道路应该是:

edit_comment_reply_path(@comment, @reply)

答案 1 :(得分:0)

@约翰

当您使用嵌套属性时,您也必须包装表单参数。 你可以试试两个 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