URL更改为点而不是斜杠

时间:2016-10-12 13:30:01

标签: ruby-on-rails routing

我正在构建预订系统,因此用户可以查看他们的行程。预订属于一个房间,一个房间可以预订,所以@room是一个外键。我想将房间的图像链接到展示厅视图。但是,当我点击该链接时,网址将更改为c9users.io/ rooms.1 ,而不是c9users.io/ rooms / 1

使这项工作需要哪些步骤?

导致此行为的原因是什么?

它看起来不像复数错误,因为我也尝试过:

<%= link_to room_path(trip.room) do %> 

不幸的是结果是一样的。

your_trips.html.erb

<% @trips.each do |trip| %>
        <%= link_to rooms_path(trip.room) do %>
            <div>
                <%= image_tag trip.room.photos[0].image.url(:thumb) if trip.room.photos.length > 0 %>
            </div>
        <% end %>
<% end %>

的routes.rb

Rails.application.routes.draw do

  root 'static_pages#welcome'

  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  devise_for :users,
             :path => '',
             :path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
             :controllers => {:omniauth_callbacks => 'omniauth_callbacks'}

  get 'rentout' => 'static_pages#rentout'
  get 'about' => 'static_pages#about'
  get 'impressum' => 'static_pages#impressum'

  resources :rooms, only: [:index, :show]
  resources :photos

  resources :rooms do 
    resources :reservations, only: [:create]
  end

  get '/preload' => 'reservations#preload'
  get '/preview' => 'reservations#preview'

  get '/your_trips' => 'reservations#your_trips'
end

reservations_controller.rb

class ReservationsController < ApplicationController
    before_action :authenticate_user!

    def preload
        room = Room.find(params[:room_id])
        today = Date.today
        reservations = room.reservations.where("start_date >= ? OR end_date >= ?", today, today)

        render json: reservations
    end

    def preview
        start_date = Date.parse(params[:start_date])
        end_date = Date.parse(params[:end_date])

        output = {
            conflict: is_conflict(start_date, end_date)
        }

        render json: output
    end

    def create
        @room = Room.find(params[:room_id])
        @reservation = current_user.reservations.create(reservation_params.merge(room_id: @room.id))
        redirect_to @reservation.room, notice: "Your reservation has been created"
    end

    def your_trips
        @trips = current_user.reservations
    end

    private
        def is_conflict(start_date, end_date)
            room = Room.find(params[:room_id])

            check = room.reservations.where("? < start_date AND end_date < ?", start_date, end_date)
            check.size > 0? true : false
        end

        def reservation_params
            params.require(:reservation).permit(:start_date, :end_date, :price, :total, :room_id)
        end
end

rooms_controller.rb

class RoomsController < ApplicationController
  before_action :set_room, only: [:show]

  def index
    @rooms = Room.all
  end

  def show
    @photos = @room.photos
  end

  private
    def set_room
      @room = Room.find(params[:id])
    end
end

我正在使用Rails 5.0.1和Cloud9 IDE。

1 个答案:

答案 0 :(得分:1)

您似乎已将rooms资源定义了两次:

resources :rooms, only: [:index, :show]
resources :photos

resources :rooms do 
  resources :reservations, only: [:create]
end

您可以合并:

resources :photos

resources :rooms, only: [:index, :show] do 
  resources :reservations, only: [:create]
end

我并非100%确定会解决您的问题,但这是我尝试的第一件事。