user16165663
user16165663

Reputation:

Ruby Sinatra URL keeps appending

I have four buttons, and everytime I click the button it should send a post request (I'll show my code below).

This is my books controller.rb

post '/all?' do
    @books = # Books here
    haml :'books/index', locals: {books: @books}
  end

  post '/fantasy?' do
    @fantasy = # Fantasy books here
    haml :'books/index', locals: {fantasy: @fantasy}
  end

And this is my index.haml file:

%form{method: 'POST', action: "books/all"}
   %input.btn.btn-md{type: 'submit', value: 'All Books'}
%form{method: 'POST', action: "books/fantasy"}
   %input.btn.btn-md{type: 'submit', value: 'Fantasy'}

However, what happens is the following: (1) URL: localhost:8080/books (2) I click on the all button (3) URL: localhost:8080/books/all (4) I click on the fantasy button (5) URL: localhost:8080/books/books/fantasy

This is the problem - how can I get it so on the second button click, it goes to localhost:8080/books/fantasy instead? I'm sure its a simple fix just not sure how. Thanks.

Upvotes: 0

Views: 36

Answers (1)

Casper
Casper

Reputation: 34308

I think the recommended way is to use absolute paths with the addition of the url helper. This should better future-proof your code in case it is mounted in a sub folder or behind a reverse proxy.

%form{method: 'POST', action: url("/books/fantasy")}

Upvotes: 1

Related Questions