2017-11-25 10 views
0

がすくいルートがそれぞれルビーon Railsのルートの変更は

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 
から変更ルーティングを呼び出した後

resources :comments do 
resources :replies 
end 

resources :comments 
resources :replies 

から私のroutes.rbをを変更しようとするとエラーにつながります

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' 

EDIT: パスは新しいエラーが返信に/ _form.erbを発生し

comment_edit_reply_pathするためにそれを変更することにより固定した。

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

についてです私はあなたがのように、ネストされたルートを追加しようとしていると思います

ので、あなたのパスがすべき:

edit_comment_reply_path(@comment, @reply) 
+0

ような何かを宣言する必要があり、ここで

<%= form_for [:comment, @reply] do |form| %> 

のform_for使い方モデル:@reply、url:[@comment、@reply]) –

+0

form_forを使いたい場合は、<%= form_for [@comment、@reply] do%> –

+0

それはあなたの提案の両方のための '未定義のメソッド' replies_path 'を与える​​ – John

0

@ジョン

あなたもフォームパラメータをラップする必要がありますので、あなたは、ネストされた属性を使用しているAS。 あなたは

<%= form_with(model: reply, url: comment_replies_path(@comment), local: true) do |form| %> 

2 form_with使用)の両方 1を試みることができる)@ [ユーザー名]新しい方法であなたは(form_with使用することができ、この

@reply = Reply.new 
+0

私はフォームの部分を解決したと思いますが、コントローラで問題が発生しました - '@reply = @ comment.replies.create(reply_params).permit(:reply)' NoMethodError @SSR – John

+0

@John 2つのモデル間の関係を確認してください。 – SSR

関連する問題