Ruby on rails 将_链接到动作“显示”到另一个控制器 ruby 1.9.2p290 轨道3.1.1
基本上我有两种模式:厨师和食谱Ruby on rails 将_链接到动作“显示”到另一个控制器 ruby 1.9.2p290 轨道3.1.1,ruby-on-rails,ruby-on-rails-3.1,link-to,Ruby On Rails,Ruby On Rails 3.1,Link To,基本上我有两种模式:厨师和食谱 class Chef < ActiveRecord::Base has_many :recipes end class Recipe < ActiveRecord::Base belongs_to :chef end 有了这些,我就有了我想要的URL: /配方-配方列表 /厨师/用户名/食谱-厨师食谱列表 /厨师/厨师名单 /厨师/用户名-厨师个人资料 配方控制器: 我的食谱索引视图: 在中,我有到厨师配置文件的正确链接 但实际上,我有一个错
class Chef < ActiveRecord::Base
has_many :recipes
end
class Recipe < ActiveRecord::Base
belongs_to :chef
end
有了这些,我就有了我想要的URL:
/配方-配方列表
/厨师/用户名/食谱-厨师食谱列表
/厨师/厨师名单
/厨师/用户名-厨师个人资料
配方控制器:
我的食谱索引视图:
在中,我有到厨师配置文件的正确链接
但实际上,我有一个错误的链接
我做错了什么?其中有一个奇怪的url!,您无权访问参数[:chef_id]。因此,视图中没有@chef变量。应该是零
若要避开此问题,请将您的链接更改为此
<%= link_to recipe.chef.username.capitalize, recipe.chef %>
希望这有帮助。您没有指定预期结果或当前结果。对不起,可能我没有正确解释我的英语不太好。我想在菜谱控制器的索引视图中找到厨师表演动作/厨师/用户名的链接。Phyo,非常感谢!效果很好。急切的加载提示也是如此。我是新手!为什么你认为url很奇怪?通常是这样或那样。也许你改变了你的主机文件?当然,你是对的。出于某种原因,我省略了localhost:
def index
@chef = Chef.find_by_username(params[:chef_id])
@recipes = Recipe.where({ :status_id => 1 }).order("id desc").page(params[:page]).per(9)
end
def index_chef
@chef = Chef.find_by_username(params[:chef_id])
@recipes = @chef.recipes.where(:status_id => 1).order("id desc").page(params[:page]).per(9)
end
<%= link_to recipe.chef.username.capitalize, @chef %>
<%= link_to recipe.chef.username.capitalize, recipe.chef %>
@recipes = Recipe.where({ :status_id => 1 }).includes(:chef).order("id desc").page(params[:page]).per(9)