pass variable from html to ruby on rails -
i new in ruby on rails , want pass variable index.html.erb
search.html.erb
file , use variable in ruby code. in other words, have text , submit button in index.html.erb
. if type in text , press button pass in text search.html.erb
, use text in ruby code
index.html.erb
<h1>twitter</h1> <html> <form> <input type = 'search' name = 'search' /></br> <a href=/home/search><button type="button">search</button></a> </form> </html>
search.html.erb
<h1>twitter</h1> <html> <%= require 'twitter' client = twitter::rest::client.new |config| config.consumer_key = "xxxxxxxxxxxxxxxxxx" config.consumer_secret = "xxxxxxxxxxxxxxxxxx" config.access_token = "xxxxxxxxxxxxxxxxxx" config.access_token_secret = "xxxxxxxxxxxxxxxxxx" end %> <ul> <%=client.search("**i want variable here**", result_type: "recent").take(5).collect |tweet| %> <li><%= "#{tweet.user.screen_name}: #{tweet.text}" %> </li> <% end %> </ul> </html>
thank you
first of need have erb structured properly,
<% form_tag("/home/search", method: "get") %> <%=text_field_tag(:search)%> <%=submit_tag("search")%> <%end%>
you can send local variable in controller renders search.html.erb, this,
get '/hello/search' search_text = params[:search] render :search, :locals => {:your_variable => search_text} end
now can use variable in views/search.html.erb ,
<%=client.search(your_variable, result_type: "recent").take(5).collect |tweet| %>
Comments
Post a Comment