rubyとrailsを勉強し始めたものです。form_forのpathを作成していますが、うまくいかず、基本的な理解を確認しておきたいと考えています。
環境 ruby 2.3.0 rails 5.0.2
やりたいこと 予約画面を作成しており、以下の3つの画面を作成しております。 ①日付の選択画面(new.html.erb) ②日付の編集画面(update.html.erb) ③選択した日付の確認画面(show.html.erb)
この予約画面は、listing_controller.rbを親として、reservation_controller.rbを子とする構造となっています。 ◾️listing.rb class Listing < ApplicationRecord has_many :reservations end ◾️reservation.rb lass Reservation < ApplicationRecord belongs_to :listing end
ルートは、以下のように作成しております。 routes.rb resources :listings do resources :reservations, only: [:new, :create, :show, :update] end
さて、確認したいことは、以下の2点になります。
①update.html.erbでform_forを以下のように書いておりますが、ページの遷移がうまくいきません。
どの点が誤りになりますでしょうか?特にエラー表示はされず、元のupdate.html.erbにとどまったままになっていますし、データが保存されておりません。
<%= form_for [@listing, @listing.reservations] do |f| %> このように書い田理由は、reservations#updateのパスが以下のようになっていたためです。 ただ、私は以下のパスで、どのようにオブジェクトをform_forで渡せば良いのかがわかりません。 /listings/:listing_id/reservations/:id(.:format)
reservation_controller.rbは以下のようになっています。※updateの部分のみ抜粋
def update @listing = Listing.find(params[:listing_id]) @reservation = Reservation.find_by(params[:listing_id]) if @reservation.update(reservation_params) redirect_to :back, notice:'更新できました。' end end private def reservation_params params.permit(:start_date, :reservation_time, :reservation_message, :price_setting, :total_price, :listing_id) end
②つぎに、show.html.erbからedit.html.erbに飛ばすボタンを作成しておりますが、これもページ遷移がうまくいっておりません。これもエラーが出るわけではないですが、ページが遷移しないということになります。
show.html.erb <div class="col-md-6"> <button type="button" class="btn btn-warning btn-block"> <% @listing.reservations.each do |reservation| %> <%= link_to "予約を変更する。", listing_reservation_path(@listing, reservation) %> <% end %> </button> </div>
ルートは以下のようになっております。 listing_reservation GET /listings/:listing_id/reservations/:id(.:format) reservations#show PATCH /listings/:listing_id/reservations/:id(.:format) reservations#update
恐らく私が、routesを理解しきれていないことから生じていると思いますが、考え方も含めて教えていただければ幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/30 03:41 編集
2017/11/04 02:53