Dynamic fields using Sinatra

Asked

Viewed 67 times

0

In Rails this is a relatively simple task. But I couldn’t find anything in the Sinatra documentation that said it was possible. I want to add fields to a form dynamically. User clicks on a link and a new field is added. If you wish, click on another link and the field is removed. On the front end the task is super quiet, I know. But to persist an arbitrary number of fields in the database? What would it be like?

  • From what I understand, each added field will be one registration and not a spine from a table, right? That wasn’t very clear.

1 answer

1

Documentation says Sinatra also supports query Parameters optional:

Routes may also use query Parameters:

get '/posts' do
  # matches "GET /posts?title=foo&author=bar"
  title = params[:title]
  author = params[:author]
  # uses title and author variables; query is optional to the /posts route
end

Source: http://www.sinatrarb.com/intro.html

So you can use one if simple to find out if a parameter exists:

require "sinatra"

get "/foo" do
  params[:bar] if params[:bar]
end

In the above example:

localhost:5678/foo            # retorna uma página em branco
localhost:5678/foo?bar=foobar # retorna "foobar"

So I think one way to solve your problem would be to create each field with a number on the back:

<input type="text" name="campo1"/>
<input type="text" name="campo2"/>
<input type="text" name="campo3"/>
<input type="text" name="campo4"/>

And then you could use a loop loop to check if the field exists and do what you need with it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.