Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get params attributes in post?

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?

like image 357
dt1000 Avatar asked Apr 07 '11 23:04

dt1000


People also ask

How do you find POST parameters?

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.

Can I use params in POST request?

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.

Can POST have params?

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.

Can HTTP POST have parameters?

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.

How do we get the params from the URL?

For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.

How do I get params for API?

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.


1 Answers

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.

like image 67
Todd Yandell Avatar answered Oct 13 '22 20:10

Todd Yandell