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 🙂
You just need to call the model :
And add the ‘self’ keyword:
But I think you should set the type in a
before_createstatement: