【发布时间】:2016-05-11 02:13:30
【问题描述】:
在我的显示页面中,我想根据变量的值更改“查看更多”按钮的路径。例如,如果我在显示页面上查看位于佛罗里达州坦帕市的一座建筑物并单击“查看更多”,我想返回到 locations_tampa_path 以再次查看坦帕市建筑物的完整列表。但是,我希望链接中的路径根据该特定建筑物的城市进行更改:
类似的东西:location_#{@location.city}_path
最好的方法是什么?
提前感谢您提供的任何帮助。
我的控制器:
class LocationsController < ApplicationController
def index
@locations = Location.all
end
def new
@location = Location.new
end
def create
@location = Location.new(location_params)
if @location.save
flash[:notice] = "New location added"
redirect_to root_path
else
flash.now[:error] = 'Cannot send message'
render 'new'
end
end
def jacksonville
@locations = Location.where(:city => "Jacksonville")
end
def stpetersburg
@locations = Location.where(:city => "St. Petersburg")
end
def orlando
@locations = Location.where(:city => "Orlando")
end
def tampa
# @location = Location.find(params[:id])
@locations = Location.where(:city => "Tampa")
@photo = Photo.new
end
def show
@location = Location.find(params[:id])
@photo = Photo.new
end
private
def location_params
params.require(:location).permit(:name, :description, :address, :city, :featured)
end
end
路线
get 'locations/tampa', to: 'locations#tampa'
get 'locations/jacksonville', to: 'locations#jacksonville'
get 'locations/orlando', to: 'locations#orlando'
get 'locations/st_petersburg', to: 'locations#stpetersburg'
resources :locations do
resources :photos, only: :create
end
【问题讨论】:
标签: ruby-on-rails