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

Question

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

Answers 1
Added at 2016-12-18 12:12
Tags
Question

I am trying to make simple webboard application. In my question/show view, I have get method to link for action to answer_controller. But it seems not link to the answer view. The error is Sinatra does not know this path. I want to create the path from question/show.erb view to answer/new.erb via submit answer button. What should I do?

question_controller (Controller folder)

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

question/show (View folder)

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

answer_controller (Controller folder)

get 'answers/question/:q_id/new' do
erb :'answer/new'
end
Answers
nr: #1 dodano: 2016-12-18 16:12

You just need the leading slash in the url of your answer route:

get '/answers/question/:q_id/new' do
  # ...
Source Show
◀ Wstecz