dt1000
dt1000

Reputation: 3732

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?

Upvotes: 16

Views: 32597

Answers (2)

Pradeep S
Pradeep S

Reputation: 2347

Further to Todd answer, you might want to get all params in an instance var i.e

@params = params 

& then in the view

you can do

<%=  @params[:title] %>

Upvotes: 0

Todd Yandell
Todd Yandell

Reputation: 14696

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.

Upvotes: 33

Related Questions