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