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

  • SEARCH
  • Home
  • 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 8949253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:11:04+00:00 2026-06-15T13:11:04+00:00

I could be wrong about my MVC logic, but what I’m trying to do

  • 0

I could be wrong about my MVC logic, but what I’m trying to do is take user input from a view and pass that information to the database. But before doing so, I’d like to determine the type of data that was submitted by analyzing some regex (and then passing the type to the database as well as the content).

But for some reason I’m getting an error (undefined method `get_type’) that the method I’m calling from the model doesn’t exist. Am I wrong in thinking that this method should be in the model?

Controller:

  def create
    @post = Post.new(
      content: params[:post][:content]
      type: get_type(params[:post][:content])
    )
    @post.save
  end

Model:

  def get_type
    if self.content =~ /(\.jpg|\.png|\.bmp|\.gif)$/
      return 'image'
    end
  end

Giant disclaimer: I just started ruby (and rails for that matter) a few days ago 🙂

  • 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-15T13:11:04+00:00Added an answer on June 15, 2026 at 1:11 pm

    You just need to call the model :

      def create
        @post = Post.new(
          content: params[:post][:content]
          type: Post.get_type(params[:post][:content])
        )
        @post.save
      end
    

    And add the ‘self’ keyword:

      def self.get_type(content)
        if content =~ /(\.jpg|\.png|\.bmp|\.gif)$/
          return 'image'
        end
      end
    

    But I think you should set the type in a before_create statement:

    class Post < ActiveRecord::Base
      #...
      before_create :set_type
      #...
    
      def set_type
        if self.content =~ /(\.jpg|\.png|\.bmp|\.gif)$/
          self.type = 'image'
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could be wrong about this, but it is my understanding that it is
This may be a dumb question, but in MVC the view doesn't know about
I am not sure what I could be doing wrong that causes info level
Could someone explain what I am doing wrong with my classes that my JTabbedPane
I was wondering if someone could tell me what I am doing wrong that
I need to pass a url string from the view to the controller, a
In some MVC frameworks you can call controller action from the view if you
Edit: I am trying to bind a single view model object that contains a
What could possibly go wrong with the following transaction if executed by concurrent users
OpenFileDialog^ dialog = gcnew OpenFileDialog; dialog->ShowDialog(); What could possibly be wrong with this? I'm

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.