Ruby on Rails 5で、ルーティング用の追加パラメーターを指定しながら、新しいアクションと編集アクションに同じform_forpartialを使用するにはどうすればよいですか?

chrickso

私のroutes.rbには、

scope ':account_id/' do
  resources :notes
end

これにより、URLは次のようになります。

                notes GET    /:account_id/notes(.:format)     notes#index
                      POST   /:account_id/notes(.:format)     notes#create
             new_note GET    /:account_id/notes/new(.:format)                                                         notes#new
            edit_note GET    /:account_id/notes/:id/edit(.:format)                                                    notes#edit
                 note GET    /:account_id/notes/:id(.:format)                                                         notes#show
                      PATCH  /:account_id/notes/:id(.:format)                                                         notes#update
                      PUT    /:account_id/notes/:id(.:format)                                                         notes#update
                      DELETE /:account_id/notes/:id(.:format)                                                         notes#destroy

私の_form.html.erbには、次のものがあります。

<%= form_for(@note, url: notes_path(account_id: @account.hash_id, id: @note)) do |form| %>

これはNewで正常に機能し、編集するフォームを正しく生成しますが、編集フォームに入力するアクションは、私が指定する「notes_path」であり、必要な「edit_note_path」ではありません。

:account_idを指定して、form_forが重複する_formパーシャルを持たずに、必要なルートを正しく生成するようにする方法はありますか?1つは「url: notes_path(...」を指定し、もう1つは「」に変更しurl: edit_note_path(...ますか?

マニシュ

以下のように編集するためのパスを指定できます。

= form_for @note, :url => @note.persisted? ? edit_notes_path(account_id: @account.hash_id, id: @note) : notes_path(account_id: @account.hash_id, id: @note) do |f|

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ