gpt4 book ai didi

ruby-on-rails - link_to 路径定义

转载 作者:行者123 更新时间:2023-12-03 00:49:52 27 4
gpt4 key购买 nike

我正在开发一个 Rails v2.3.2 应用程序。

我有一个 Controller :

class SchoolController < ApplicationController
...

def edit
@school=School.find_by_id params[:id]

end

def check_teachers
@teachers = @school.teachers
...
end

end

app/views/schools/edit.html.erb中我想要一个链接,点击它会触发check_teachers Controller 中的方法,如何定义此链接路径

app/views/schools/edit.html.erb:

link_to 'Check teachers' WHAT_IS_THE_PATH_HERE

最佳答案

link_to 'Check teachers', :action => :check_teachers, :id => @school.id

link_to 'Check teachers', "/school/check_teachers/#{@school.id}"

或者您可以在 config/routes.rb 中定义命名路由,如下所示:

map.check_teachers, '/school/check_teachers/:id' :controller => :school, :action => :check_teachers

并调用由命名路由生成的 url-helper,如下所示:

link_to 'Check teachers', check_teachers_path(:id => @school.id)

你可以使用这个id在 Controller 中查找老师

def check_teachers
@school = School.find params[:id]
@teachers = @school.teachers
...
end

关于ruby-on-rails - link_to 路径定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8477301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com