Я новичок в Rails, и немного запутался в маршрутах:
У меня есть контроллер устройств:
#devices_controllers.rb
class DevicesController < ApplicationController
def index
@devices = Device.all
end
def show
@device = Device.find(params[:id])
end
def new
@device = Device.new
end
def create
@device = Device.new(params[:device])
if @device.save
flash[:notice] = "Successfully created device."
redirect_to @device
else
render :action => 'new'
end
end
def edit
@device = Device.find(params[:id])
end
def update
@device = Device.find(params[:id])
if @device.update_attributes(params[:device])
flash[:notice] = "Successfully updated device."
redirect_to @device
else
render :action => 'edit'
end
end
def destroy
@device = Device.find(params[:id])
@device.destroy
flash[:notice] = "Successfully destroyed device."
redirect_to devices_url
end
def custom_action
"Success"
end
Я бы хотел получить доступ к действию "custom_action" через URL-адрес, подобный следующему:
http://foo.bar/devices/custom_action
Я добавил эту строку в свой файл rout.rb:
match 'devices/custom_action' => 'devices#custom_action'
Однако, когда я пробую URL в браузере, я получаю эту ошибку:
ActiveRecord::RecordNotFound in DevicesController#show
Couldn't find Device with ID=custom_action
Похоже, вместо #custom_action происходит действие #show. Если идентификатор пользователя не указан, и я перехожу на http://foo.bar/devices/custom_action
, я бы хотел, чтобы он прошел # custom_action.
Я прочитал Маршрутизация с внешней стороны , но все еще не могу решить проблему.