0

I am new in Ruby on Rails and I want to pass variable from index.html.erb to search.html.erb file and use the variable in ruby code. In other words, I have text and submit button in index.html.erb. If I type in text and press the button it will pass what in the text to search.html.erb and use that 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 do |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 do |tweet| %>
      <li><%=  "#{tweet.user.screen_name}: #{tweet.text}" %> </li>   
    <% end %>
   </ul>
</html>

Thank you

2
  • Try not to do that inside your view. You should keep stuff like that in your controller, pass it through as @client. Commented Nov 10, 2016 at 8:10
  • 2
    It seems that you're really new and don't have the foggiest idea how rails app works, how it should be organized, how data flows, etc. While I believe in "learning by doing", this is not going to work well here. You'll have much easier time if you complete the rails tutorial first. Or a good rails book (Agile Web Development with Rails, for example). If you don't, you're bound to struggle every part of the way. Structured knowledge is not to be underestimated. Commented Nov 10, 2016 at 8:18

4 Answers 4

1

First of all you need to have your ERB structured properly,

<% form_tag("/home/search", method: "get") do %>
    <%=text_field_tag(:search)%>
    <%=submit_tag("Search")%>
<%end%>

You can send the local variable in the controller that renders the search.html.erb, something like this,

get '/hello/search' do
    search_text = params[:search]
    render :search, :locals => {:your_variable => search_text}
end

now you can use this variable in views/search.html.erb ,

<%=client.search(your_variable, result_type: "recent").take(5).collect do |tweet| %>
Sign up to request clarification or add additional context in comments.

Comments

1

you can use form to pass the data.

<%form_for 'url_for_search_page_here' do %>
    <%=text_field_tag :search%>
    <%=submit_tag, "Search"%>
<%end%>

by using the form tag you got the params[:serach] with value in your search action or where you to submit you form.

Comments

1

You have to mention action parameter in your form which goes to that action with the user entered parameters. Inside that action you can get the parameters and use in your html pages.

First of all I suggest to use ruby form helper methods instead of plain html because you can easily build your forms (You can use ruby gems like "simple_form")

<%= form_for "<search_url_here>" do |f| %>
  <%= f.label :search %>
  <%= f.text_field :search %>
  <%= f.submit %>
<% end %>

In your search method

def search
  @search_term = params[:search]
end

In your search.html.erb page you can access as @search_term

3 Comments

when I type and then press search to go to search.html.erb, I get this errer"No route matches [POST] "/home/index" in route.rb I have get 'home/index', to: 'home#index' get 'home/search', to: 'home#search'
In general form will take default method as "post". the request is entitled as post, when user input is being posted to the server with user entered parameters. So in your routes.rb file you have to mention: post 'login' => 'sessions#create'
@Mohammed here in your routes.rb you need to write: post 'search' => 'home#search'
0

First of all, it will be much better if you add the twitter configuration in an initializer file and make it accessible globally. As much as possible, try to avoid having too much logic on your view.

e.g.

# config/initializers/twitter.rb
$twitter_client = Twitter::REST::Client.new do |config|
             config.consumer_key = ENV['CONSUMER_KEY']
             config.consumer_secret = ENV['CONSUMER_SECRET']
             config.access_token = ENV['YOUR_ACCESS_TOKEN']
             config.access_token_secret = ENV['YOUR_ACCESS_SECRET']
           end

while the search form should look like this as Pardeep Saini advised

#app/views/index.html.erb

<%= form_tag 'your_path_for_search' do %>
  <%=text_field_tag :search%>
  <%=submit_tag, "Search"%>
<% end %>

then on your search controller and view

#app/controllers/search_controller.rb

def search
  @result = $twitter_client.search(params[:search], result_type: "recent").take(5)
end

#app/views/search.html.erb
<ul>
  <%= @result.each do |tweet| %>
    <li><%=  "#{tweet.user.screen_name}: #{tweet.text}" %> </li>   
  <% end %>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.