How to use parameterize in Rails? - ruby-on-rails

I want to convert the title of a page to a friendly URL and store it in the database as a permalink. My problem is I can't use the parameterize method. It's not working. Other inflections are working like upcase or downcase but parameterize is not working. Is there a special case for parameterize?
This is my code:
Controller:
def create
params[:page][:permalink] = params[:page][:title].dup
#page = Page.new(params[:page])
end
Model:
class Page < ActiveRecord::Base
before_save :makeitpermalink
before_update :makeitpermalink
private
def makeitpermalink
permalink.parameterize!
end
end

According to the Rails' documentation, there is no bang (exclamation mark) version of the parameterize method, so try removing it:
def make_it_permalink
self.permalink = self.permalink.parameterize
end

Related

Rails5 - Ordering with Class method and params

In Rails, I want to order some tweets by number of likes desc using paramsand I am stuck..
I wrote a simple Class method in my model:
class Twit < ApplicationRecord
def self.most_liked
order(like: :desc)
end
end
And here is my method in my controller:
class TwitsController < ApplicationController
def index
if params[:filter]
#twits = Twit.most_liked(params[:filter])
else
#twits = Twit.all
end
end
end
If I only use #twits = Twit.most_liked, it works fine, but when I add my condition with params it fails.
Thank you !
It can't work, because you try use never created metod. You created
def self.most_liked
order(like: :desc)
end
But later you call 'most_liked(bla-bla)'.
You should:
Add param to your method:
def self.most_liked(var1)
Or create method:
def most_liked(var1)
Twit.where(x=>var1).order(like: :desc)
end
Or change call of this method to:
Model.query.method - so:
Twit.where(params[:filter]).most_liked
It fails because you defined method self.most_liked without parameters, and then you try to pass a parameter Twit.most_liked(params[:filter]).
You should define method
def most_liked(some_variable)
or chain it with another method like this:
Twit.where(params[:filter]).most_liked

Rails - How to set regex as method?

I have a regex that I want to use for word count and validation. However, I don't want to have to go into every model and controller if I decide to change the regex I'm using. Is there a way to create a helper method that defines the regex?
This is what I've tried and it doesn't work:
module ApplicationHelper
def word_count(content_text)
content_text.scan(scan_regex).size
end
def scan_regex
return "/\b(([a-zA-Z’'-]))+((?![^<>]*>)+(?![^&;]*;))\b/"
end
end
You could remove the '/' from the regexp and then add them to scan:
module ApplicationHelper
def word_count(content_text)
content_text.scan(/#{scan_regex}/).size
end
def scan_regex
return "\b(([a-zA-Z’'-]))+((?![^<>]*>)+(?![^&;]*;))\b"
end
end

Is there are way that model methods can work with variable with out them being explicitly passed?

I know this sounds like a ridiculous question but I trying to solve a chalange given by an potential employer. I have a schema and a couple of models with their methods. Almost all the methods have no variables passed in. Meaning none of the methods look like this:
def this_is_my_method(variable)
#stuff
end
or
def this_is_my_method variable
#stuff
end
but there are methods that are clearly working with variables like this:
def build_address
if variable
# do something
end
end
Is there a RoR way that a model method will just know about certain parameters or variables in certain situations?
So if my controller was recieving params that looked like this:
?my_method[begin]=1&my_method[end]=5
would the model method "my_method" know what "begin" and "end" where?
def my_method
if self.begin == self.end
# do something
else
# do something else
end
end
Remember that a model method has access to all the attributes (and other methods) of that model instance.
So (for example) this would be a valid model method.
class User < ActiveRecord::Base
def full_name
[first_name, last_name].join(' ')
end
end
This is taking an attribute user.first_name and an attribute user.last_name and combining them to create a new method user.full_name
EDIT as #Sanket has suggested you can pass values into a model if you make them attribute accessor...
def SomeController < ApplicationController
def some_controller_method
#user = User.find(params[:id])
#user.begin = params[:begin]
#user.end = params[:end]
#user.some_model_method
end
end
def User < ActiveRecord::Base
attr_accessor :begin, :end
def some_model_method
if self.begin == self.end
# do something
else
# do something else
end
end
end
Although to be frank I'd rather just pass the values in as method arguments.

Using after_find callback to overwrite the field values

I have rails 2.3.11. i want to overwrite one of the database field value.But its not overwrite.
def after_find
add_public_uri
end
def add_public_uri
self.uri = uri.to_s
end
Not sure about the syntax, but have you tried
after_find: add_public_uri
private
def add_public_uri
self.uri = uri.to_s
end
Alternatively, you could simply have a customer reader in your model:
def uri
uri.to_s
end

undefined method `parameterize' for nil:NilClass

I've been trying to do SEO friendly urls, and managed to get it work, but when I call index action on blogs, I get a weird "undefined method `parameterize' for nil:NilClass." The method works when using show method.
#model
def to_s
title
end
def to_param
"#{id}-#{to_s.parameterize}"
end
#controller
#blogs = Blog.find.all
Screenshot of error
http://www.freeimagehosting.net/image.php?83e76a260b.png
Turns out you can't call title.parameterize on to_param without error. So I added a permalink column and called parameterize on that.
#models/blog.rb
before_save :permalink
def to_param
"#{id}-#{permalink}"
end
def permalink
self.permalink = self.title.parameterize
end
And voila. I knew it was something really stupid.
In case anyone is having trouble with this in Rails 5....I left out the #to_s part, which is critical.
class Post < ApplicationRecord
def to_param
slug
end
def slug
"#{id}-#{pretty_url}"
end
def pretty_url
title.to_s.parameterize
end
end

Resources