I’m trying to write a very simple function that feeds whatever text someone enters into an input field, into a function which searches Twitter and returns some value.
In my home.html.erb file I have this code, but I’m not sure if calling the @tweetArray instance variable which should call my grabTweets function is the right way to go about things.
<%= form_for(@tweetArray) do |f| %>
<%= f.text_field :grabTweets %>
<% end %>
In my pages helper file I have:
@tweetArray = grabTweets(:grabTweets)
def grabTweets(mySearch)
@tweet = Twitter.search( "mySearch +" "[pic] "+" path.com/p/", :rpp => 2, :result_type => "recent").map do |status|
@tweet = "#{status.text}" #class = string
urls = URI::extract(@tweet, "http") #returns an array of strings
end
end
My page just shows this error undefined method grabTweets for PagesHelper:Module. What do I put in my home.html.erb file to call this code correctly?
Edit:
Updated my code in home.html.erb
<%= form_for(grabTweets) do |f| %>
<%= f.text_field :search %>
<% end %>
Does that make more sense?
And then in my pages controller
@tweetArray = grabTweets(":search")
def grabTweets(mySearch)
@tweet = Twitter.search( mySearch + "[pic] "+" path.com/p/", :rpp => 2, :result_type => "recent").map do |status|
@tweet = "#{status.text}" #class = string
urls = URI::extract(@tweet, "http") #returns an array of strings
end
end
I’m still getting an error undefined local variable or method grabTweets in home.html.erb
With a Model
Assuming you have an @tweet object:
Then, depending on how you have your routes set up, when a user submits the form it will hit your
find_tweetsaction. You could have a route set up for/tweetsthat hits thefind_tweetsaction (call it what you like).In your controller you’d have:
That should get you an
@tweetsthat would an array of hashes with a tweet and urls. I haven’t tested any of this but I hope it’ll point you in the right direction.Without a Model
To do this without a model you can use
form_tag.Your controller wouldn’t change much, but you’d likely access your search params using
params[:search]instead.