Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:33+00:00 2026-06-13T09:49:33+00:00

I have a method in one of my models that, when called, fetches a

  • 0

I have a method in one of my models that, when called, fetches a tweet using the twitter gem and stores some parts of it. I’d like to be able to trigger that action from the web interface to my app. What is the Rails Way to accomplish this? I’ve seen some references to not calling model methods from views, so should I be doing this from within a controller somehow instead?

My method (the relevant models are Sponsor and Sponsortweet (so my model name wouldn’t conflict with Tweet, from the gem):

def create_tweet                                                                  
    tweet = Twitter.user_timeline(self.twitter).first                               
    self.sponsortweets.create!(content: tweet.text,                                 
                               tweet_id: tweet.id,                                  
                               tweet_created_at: tweet.created_at,                  
                               profile_image_url: tweet.user.profile_image_url,     
                               from_user: tweet.from_user,)                         
end

EDIT:

I created a tweet method in my sponsors controller:

def tweet
  @sponsor = Sponsor.find(params[:id])
  @sponsor.create_tweet
end

and added the following to my config/routes.rb: match 'tweet', to: 'sponsors#tweet', via: :post.

As well as the following code in my view (I’m using haml):

= button_to :tweet, tweet_path(@sponsor)

However, clicking the button results in the following error:

Couldn't find Sponsor without an ID

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:49:34+00:00Added an answer on June 13, 2026 at 9:49 am

    Your view should have a button that posts to a specific route in your controller. That controller would then call the method in your model. Having no idea what your app actually looks like, here’s an example:

    EDIT includes better example

    View (assuming it’s a Sponsor view):

    <%= button_to :submit, tweet_path %>
    

    Controller:

    def tweet
        Sponsor.create_tweet
    end
    

    And your model would stay the same, except you’d change your method to a class method like so:

    def self.create_tweet
        ...your code here...
    end
    

    Since it seems this isn’t tied to any particular sponsor, you’ll use a class method and thus don’t need an instance of the class to call your method. That said, it seems like you would want an instance of your class at some point…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
I have one method that opens a file and passes off to another function
I have a project with a lot of panels. I have one method that
I have an InteferField set in models called the status. I have a method
In my rails application my models have a to_html method. This method is called
I have some 3D models that I render in OpenGL in a 3D space,
suppose I have a model and a view ,ths view have two method:one is
I have one method in my managed bean which returns javascript as a string.
I have one method with one letter(string\char?) as argument and one word active at
I have one combobox on the window form and I have one method which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.