Ruby on rails (Rails)当有人单击指向的链接时,试图显示正确的项目
您好,我还是Rails的新手,但目前我已经在一个Rails项目上工作了一段时间,我的最后一个问题是当有人点击一个特定的配方时,它只显示了用户创建的第一个配方。我已经通过控制台访问了我的数据库,查看这些配方是否正在保存,但当我单击指向特定配方的任何链接时,它仍然会显示错误的配方,并且也不会显示配方名称 这是我的食谱控制器Ruby on rails (Rails)当有人单击指向的链接时,试图显示正确的项目,ruby-on-rails,Ruby On Rails,您好,我还是Rails的新手,但目前我已经在一个Rails项目上工作了一段时间,我的最后一个问题是当有人点击一个特定的配方时,它只显示了用户创建的第一个配方。我已经通过控制台访问了我的数据库,查看这些配方是否正在保存,但当我单击指向特定配方的任何链接时,它仍然会显示错误的配方,并且也不会显示配方名称 这是我的食谱控制器 class RecipesController < ApplicationController before_action :require_login def show
class RecipesController < ApplicationController
before_action :require_login
def show
@recipe=Recipe.find_by(params[:name])
binding.pry
end
def index
@recipes =Recipe.all
#binding.pry
end
def new
@recipe = Recipe.new
@recipe.ingredients.build(name: "name")
end
def create
@recipe = Recipe.new(recipe_params)
@recipe.save
#binding.pry
redirect_to recipes_path
end
private
def recipe_params
params.require(:recipe).permit(:id,:name,:content, ingredients_attributes: [
:recipe_id,
:user_id,
:name,
:quantity
]
)
end
class RecipesController
结束
索引页
<h1>All Recipes</h1>
<ul>
<% @recipes.each do |recipes| %>
<li><%= link_to recipes.name, recipes %></li>
<% end %>
</ul>
所有配方
显示页面
<% @recipe.name do |r| %>
<h2> <%= r.name %></h2>
<h2> <%= r.content %></h2>
<%end%>
<h3>Ingredients</h3>
<ul>
<% @recipe.ingredients.each do |ingredient| %>
<li><%= "#{ingredient.name} X #{ingredient.quantity}" %></li>
<% end %>
</ul>
成分
任何帮助都将不胜感激
谢谢大家! 在您的
show
方法中,它是以下两种方法之一
Recipe.find_by(name: params[:name])
# or ...
Recipe.find(params[:id])
…根据您的路线设置的不同,第二种是Rails通常的做事方式。在您的
show
方法中,它是其中之一
Recipe.find_by(name: params[:name])
# or ...
Recipe.find(params[:id])
…第二种方法是Rails通常的操作方式,具体取决于您在路线中的设置。您的代码存在一些问题。在
配方控制器
中,将显示
操作代码更改为:
def show
@recipe = Recipe.find(params[:id])
end
<h2><%= @recipe.name %></h2>
<h2><%= @recipe.content %></h>
<h3>Ingredients</h3>
<ul>
<% @recipe.ingredients.each do |ingredient| %>
<li><%= ingredient.name %> X <%= ingredient.quantity %></li>
<% end %>
</ul>
在index.html.erb
视图中,将迭代食谱的代码更改为:
<% @recipes.each do |recipe| %>
<li><%= link_to recipes.name, recipe %></li>
<% end %>
更改摘要
在RecipesController
的show
操作中,通过从视图传入的id搜索配方。该id来自以下行:
<%= link_to recipe.name, recipe %>
recipe
获取对其调用的to_param
,该参数返回特定配方的id,然后在recipes控制器的show
操作中使用该id来查找正确的配方
在index.html.erb
视图中,通过@recipes
变量迭代所有配方,并输出每个配方。由于要输出每个配方,因此通常使用recipe
而不是recipes
作为块变量
在show.html.erb
视图中,您不需要迭代所有配方,因为RecipesController
的show操作中只有一个配方。该配方存储在@recipe
变量中,因此您可以在视图中直接使用该变量。您的代码存在一些问题。在配方控制器
中,将显示
操作代码更改为:
def show
@recipe = Recipe.find(params[:id])
end
<h2><%= @recipe.name %></h2>
<h2><%= @recipe.content %></h>
<h3>Ingredients</h3>
<ul>
<% @recipe.ingredients.each do |ingredient| %>
<li><%= ingredient.name %> X <%= ingredient.quantity %></li>
<% end %>
</ul>
在index.html.erb
视图中,将迭代食谱的代码更改为:
<% @recipes.each do |recipe| %>
<li><%= link_to recipes.name, recipe %></li>
<% end %>
更改摘要
在RecipesController
的show
操作中,通过从视图传入的id搜索配方。该id来自以下行:
<%= link_to recipe.name, recipe %>
recipe
获取对其调用的to_param
,该参数返回特定配方的id,然后在recipes控制器的show
操作中使用该id来查找正确的配方
在index.html.erb
视图中,通过@recipes
变量迭代所有配方,并输出每个配方。由于要输出每个配方,因此通常使用recipe
而不是recipes
作为块变量
在show.html.erb
视图中,您不需要迭代所有配方,因为RecipesController
的show操作中只有一个配方。该配方存储在@recipe
变量中,因此您可以在视图中直接使用该变量