如何 link 在 HTML/erb 中的两个控制器和两个视图之间

How to link between two controllers and two views in HTML/erb

我正在尝试制作简单的网络板应用程序。在我的 question/show 视图中,我有方法到 link 以执行到 answer_controller 的操作。但是好像没有link的答案来看。错误是 Sinatra 不知道这个路径。我想通过提交答案按钮创建从 question/show.erb 视图到 answer/new.erb 的路径。我该怎么办?

question_controller(控制器文件夹)

get '/questions/:id/show' do
@question = Question.find_by(id: params[:id])
erb :'question/show'
end

question/show(查看文件夹)

<form method="get" controller="answers" action="/answers/question/<%=@question.id%>/new"> 
<input type=submit value="Answer"></form>

answer_controller(控制器文件夹)

get 'answers/question/:q_id/new' do
erb :'answer/new'
end

您只需要在回答路线的 url 中使用前导斜杠:

get '/answers/question/:q_id/new' do
  # ...