0
所以,我有我的應用程序設置了IDS就像這樣:獲得使用Rails中嵌套的路線和固定鏈接模型的ID
resources :studios do
resources :bookings
end
這給了我該指數的路徑(後來我要使用JSON用於獲得日曆,每個工作室。
studio_bookings GET /studios/:studio_id/bookings(.:format) bookings#index
這是一件好事,但我想擺脫的ID,並使用一個固定鏈接,而不是,只是一個友好的URL。
更改爲:
namespace :studio, :path =>'/:permalink' do
resources :bookings
end
現在我越來越
studio_bookings GET /:permalink/bookings(.:format) studio/bookings#index
太好了!這是我多麼希望我的網址看看,不過,現在的:ID是不是在任何地方的路線所以......我得到
Couldn't find Booking without an ID
它甚至沒有被通過。有沒有一種方法可以將URL傳遞給URL:URL沒有被實際使用。否則,我是否將主鍵從:id更改爲:permalink以解決此問題?
我試圖改變我的控制器從
@studio = Studio.find(params[:id])
到
@studio = Studio.find(params[:permalink])
但是這給了我
Couldn't find Booking with 'id'=40frost
還告訴我,我在做什麼是不是真正的意思做完了?它試圖把永久鏈接作爲id,所以即使我告訴rails尋找固定鏈接,它仍然看起來像是一個ID。
希望我的問題很明確:本質上 - 我如何傳遞id以便知道哪個工作室沒有在URL中顯示它。如果有一些我可以做的控制器魔法,那將會很方便。
這裏是我的好措施控制器
class Studio::BookingsController < ApplicationController
before_action :set_booking, only: [:show, :edit, :update, :destroy]
# GET /bookings
# GET /bookings.json
def index
@studio = Studio.find(params[:permalink])
@bookings = Booking.where("studio_id => '@studio.id'")
end
# GET /bookings/1
# GET /bookings/1.json
def show
end
# GET /bookings/new
def new
@booking = Booking.new
end
# GET /bookings/1/edit
def edit
end
# POST /bookings
# POST /bookings.json
def create
@booking = Booking.new(booking_params)
respond_to do |format|
if @booking.save
format.html { redirect_to @booking, notice: 'Booking was successfully created.' }
format.json { render action: 'show', status: :created, location: @booking }
else
format.html { render action: 'new' }
format.json { render json: @booking.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /bookings/1
# PATCH/PUT /bookings/1.json
def update
respond_to do |format|
if @booking.update(booking_params)
format.html { redirect_to @booking, notice: 'Booking was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @booking.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bookings/1
# DELETE /bookings/1.json
def destroy
@booking.destroy
respond_to do |format|
format.html { redirect_to bookings_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_booking
@booking = Booking.find(params[:permalink])
end
# Never trust parameters from the scary internet, only allow the white list through.
def booking_params
params.require(:booking).permit(:start_time, :end_time, :studio_id, :engineer_id, :title, :allDay)
end
end
謝謝。我採用第二種方法。 – Peege151 2014-10-17 21:48:36
這可能是謹慎的,因爲你會想使用你的ID檢索記錄更新和東西,在那裏好運! – jfornoff 2014-10-17 21:50:21