I am using Sinatra with Ruby 1.8.7. I'm new to web development, so I don't totally understand get and post, but I got some stuff working. What I need to know next is how to interrogate params in post for certain attributes. In my main file, I have this code:
get "/plan_design" do
erb :plan_design
end
post "/plan_design" do
# do stuff with params
end
In plan_design.erb, I have:
<% if (hash[paramTitle].kind_of?(String)) %>
<div> <input class="planDesignAsset" name="<%= paramTitle %>" value="<%= hash[paramTitle] %>" ></input> </div>
<% else %>
<div> <input class="planDesignAssetNum" name="<%= paramTitle %>" value="<%= hash[paramTitle] %>" ></input> </div>
<% end %>
As you can see I'm using a different class for non-strings. In post
, I need to ask params[some_key]
, what kind of class are you? Then I can treat each param accordingly. Does this make sense?
Click on the resource that you want inspect and read the request (click on the headers tab to the left of the resources list). You can see the request headers, the form data and the query string parameters.
You can send path and query parameters with your requests using the URL field and the Params tab. To send a query parameter, add it directly to the URL or open Params and enter the name and value.
POST should not have query param. You can implement the service to honor the query param, but this is against REST spec. "Why do you pass it in a query param?" Because, like for GET requests, the query parameters are used to refer to an existing resource.
There are many ways in HTTP to add parameters to our request: the query string, the body of POST, PUT and PATCH requests, and the header. Each has its own use-cases and rules. The simplest way to add in all parameter data is to put everything in the body. Many APIs work this way.
For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.
API Parameters are options that can be passed with the endpoint to influence the response. In GET requests, they're found in strings at the end of the API URL path. In POST requests, they're found in the POST body.
In Sinatra you use params
to access the form data. You should put the values you need into an instance variable, which you can access from your view:
post "/plan_design" do
@title = params[:title]
erb :plan_design
end
<input name="<%= @title %>" />
I’m not sure if this answers your question, but I hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With