LoginSignup
ayaka-k
@ayaka-k (あやか)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

(Ruby,Rails初学者)current_room.idエラー発生、日数×合計金額ができなくて困っています。。。

Q&AClosed

(Ruby,Rails初学者)current_room.idエラー発生、日数×合計金額ができなくて困っています。。。

Ruby、Rails初学者です。
宿泊予約サイトのようなものをお勉強として作成しています。
予約の部分でつまづいてしまい、調べても分からずに困っているので、助けてください。。。

(投稿しているコード以外に必要なコードがありましたら用意しますので、おっしゃってください。)

下記2点について、教えてください。

===
1、post.controller[current_room.id]でエラー発生
2、「日数×合計金額」の出し方についても教えてください。
===

step1 宿泊詳細:rooms/show のフォームで予約日数など入力 
step2 予約確認:posts/confirm *ここで合計金額を表示
step3 予約一覧:posts/index (完了)
というようなページを作っています。

テーブルは3つあります。
users(ユーザー情報) / rooms(宿泊施設の登録) / posts(予約)

1、post.controller[current_room.id]でエラー発生

postsコントローラーの[@post.room_id = current_room.id]の記述がエラーになってしまいます。

postsコントローラーの[@post.user_id = current_user.id]については、問題なく使えています。

postsのマイグレーションファイルには、[room_id][user_id]というのをカラムを付けています。

ユーザー周辺はdeviceを使用しています。
[current_user.***]というのはdeviceのときのみの使用という訳では
ないと思うのですが、なぜこれでエラーになってしまうのかわかりません。。。

これを使って、予約一覧のところで、予約したお部屋の名前などの情報も紐付けたいのでよろしくお願いします。

2、「日数×合計金額」の出し方についても教えてください。

step2 予約確認:posts/confirm *ここで合計金額を表示

この部分を実装したいのですが、1の件もあって、どう処理していいかわかりません。。。
合計金額には、postsの「fee」というカラムを用意したのでそこに入るようにしたいと考えています。

fee = room.price * (post.endday.to_date - post.startday.to_date).to_i

かな?と思ったのですが、これはどこにくっつけて実装するのか、
調べてもなかなかヒントが見つけられずに困っています。

1について、発生している問題・エラー

posts_controller
== エラーメッセージ ==
ActiveRecord::RecordNotFound in PostsController#confirm
Couldn't find Room without an ID
====================
class PostsController < ApplicationController
  def index
    @posts = Post.all
    @rooms = Room.all
  end

  def new
    @post = Post.new
    @room = Room.find(params[:id])
  end

  def confirm
    @post = Post.new(post_params)
    @room = Room.find(params[:id])
  end

  def create
    @post = Post.new(post_params)
    @room = Room.find(params[:id])
    @post.user_id = current_user.id
    @post.room_id = current_room.id  <= エラー発生箇所
    if @post.save
      redirect_to posts_path
    else
      render :confirm
    end
  end

  def show
    @post = Post.find(params[:id])
  end


  def edit
  end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to posts_path
  end


  private
  def post_params
    params.require(:post).permit(:startday, :endday, :peoples ,:fee)    
  end
end

宿泊詳細、予約フォーム↓

posts/show.html.erb
        <%= form_for @post ,url: posts_confirm_path do |f| %>
          Room.id:<%= @room.id %>
          <h5>
            <%= number_to_currency(@room.price, :unit => "円") %> / 日
          </h5>
            <hr size=".5px" color="#bbb">
          <div class="form-group">
              <%= @room.id %> <br>

              <%= f.label :room_id%>
              <%= f.text_field :room_id ,class:"profile_textfield form-control" %>

              <%= f.label :user_id%>
              <%= f.text_field :user_id ,class:"profile_textfield form-control" %>

              <%= f.label :startday%>
              <%= f.date_field :startday ,class:"profile_textfield form-control" %>

              <%= f.label :endday%>
              <%= f.date_field :endday ,class:"profile_textfield form-control" %>

              <%= f.label :peoples%>
              <%= f.text_field :peoples ,class:"profile_textfield form-control" %>

            <button type="submit" class="btn btn-primary">予約確認へ進む</button>
          </div>
        <% end %>

予約確認画面↓

posts/confirm.html.erb
<div class="create">

               <%= @room.id %>
               <img src="<%= @room.image %>" width="50">
               <%= @room.name %>
               <%= @room.introduction %>
               <%= @room.address %>

  <h4>確認画面,,</h4><br>
  <%= render "shared/error", obj: @post %>

  <div class="create_form">
  <%= @room.name %>


      <%= form_for @post do |f| %>


          Room.id:<%= @room.name %>
          <h5>合計金額  <%= number_to_currency(@room.price) %>円 / 日</h5>
            <hr size=".5px" color="#bbb">
          <div class="form-group">
              <%= @room.id %> <br>


              <%= f.label :startday%>
              <%= f.date_field :startday ,class:"profile_textfield form-control",readonly: true %>

              <%= f.label :endday%>
              <%= f.date_field :endday ,class:"profile_textfield form-control",readonly: true %>

              <%= f.label :peoples%>
              <%= f.text_field :peoples ,class:"profile_textfield form-control",readonly: true %>

              <%= f.label :fee %>
              <%= f.text_field :fee ,class:"profile_textfield form-control",readonly: true %>

            </div>


        <div class="actions">
          <%= f.submit "予約する" %>
        </div>

        <div class="actions">
          <%= f.submit "戻る", name: :back, class: "btn btn-secondary"%>
        </div>
      <% end %>
  </div>
</div>

予約一覧↓

posts/index
<h2>予約一覧</h2>

<table> 
    <thead>
        <tr>
            <th>予約者</th>
            <th>ルーム画像</th>
            <th>ルーム名</th>
            <th>ルーム紹介</th>
            <th>合計金額</th>
            <th>チェックイン</th>
            <th>チェックアウト</th>
            <th>人数</th>
            <th>予約確定日</th>
            <th>確認</th>
            <th>編集</th>
            <th>キャンセル</th>
        </tr>
    </thead>
    <tbody>

        <% @posts.each do |post| %>
            <tr>
              <td><%= current_user.name %></td>
              <td><%= post.id %></td>
              <td><%= post.id %></td>
              <td><%= post.id %></td>
              <td><%= post.fee %></td>
              <td><%= post.startday.strftime('%Y月%m日%d日') %></td>
              <td><%= post.endday.strftime('%Y月%m日%d日') %></td> 
              <td><%= number_to_currency(post.peoples, :unit => "名様") %></td>
              <td><%= post.updated_at.strftime('%Y月%m日%d日') %></td>
              <td><%= link_to "確認", post, class:"textdeco"%></td>
              <td><%= link_to "編集", [:edit, post], class:"textdeco" %></td>
              <td><%= link_to "キャンセル", post, method: :delete, data:{confirm:"本当に削除しますか?"}, class:"textdeco" %></td>

              </div>
            </tr>
              <% end %>
      </tbody>
  </table>
user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :name, presence: true 
  validates :introduction, length: { maximum: 200 } 

  mount_uploader :img, ImageUploader

  has_many :rooms, dependent: :destroy
  has_many :posts, dependent: :destroy
end

room.rb
class Room < ApplicationRecord
  belongs_to :user
  belongs_to :post

  mount_uploader :image, ImageUploader

end

post.rb
class Post < ApplicationRecord
  belongs_to :user
  belongs_to :room

  validates :startday, :endday, :peoples, :fee, presence: true 
end
schema.rb
#postsのみ抜粋
  create_table "posts", force: :cascade do |t|
    t.date "startday"
    t.date "endday"
    t.integer "peoples"
    t.integer "fee"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
    t.integer "room_id"
  end
0

No Answers yet.

Your answer might help someone💌