How do I get params attributes in post?

痴心易碎 提交于 2019-12-06 17:29:13

问题


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?


回答1:


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.




回答2:


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] %>


来源:https://stackoverflow.com/questions/5588953/how-do-i-get-params-attributes-in-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!