我是 Ruby on rails 的新手,正在制作一个带有显示页面的 Web 应用程序,该页面应该只是根据单击的链接在我的数据库中显示数据。我想让它只有一个页面可以根据我选择的食谱进行更改,但我不知道如何区分链接。
1 回答
1
这是进入控制器的显示操作的基本宁静路线。
# config/routes.rb
Rails.application.routes.draw do
get '/recipes/:recipe_id', to: 'recipes_controller#show'
end
所以你的链接可能是这样的:http ://example.com/recipes/2 应该加载一个页面,显示 id 为 2 的食谱信息
现在假设您有一个带有show操作的控制器recipes_controller.rb来为您提供有关该配方的信息
# app/controllers/recipes_controller.rb
class RecipesController < ApplicationController
...
def show
@recipe = Recipe.find(params[:recipe_id])
render 'show'
end
end
现在,在模板或您在showviews/recipes/show.html.erb
操作中呈现的任何视图中,您将可以访问包含对象值的 a。<%= @recipe %>
于 2019-04-12T00:34:35.380 回答