How to include id in slug using friendly_id - ruby

I am using friendly_id gem to have pretty urls in my application.I want an url like:
localhost:3000/posts/1/my-first-post
I want to do it using the friendly_id gem as i want to use the History module to avoid 404 error.I have tried but can't include the id in the slug.The code i am using is follows:
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :pretty_url, use: [:slugged, :history]
def pretty_url
"#{id}/#{title}"
end
end
But it is not including the id in the slug.Rather it is using the slug as if only title is used.

I think I remember reading somewhere that FriendlyId runs into issues with slashes within your slug function. Try "#{id}-#{title}" instead to see if that's the case.

The id of Post is set after the entry is saved. pretty_url doesn't work because the id doesn't exist yet.
You can get this behaviour without using the friendly_id gem by doing:
def to_param
"#{id} #{title}".parameterize
end

Related

Welcome emails in Ruby

I'm using Ruby and Devise:Confirmable. A day or so after a new user has registered and confirmed a new trial account we'd like to automatically send him or her a 'follow up email'. Is this something we should also do through devise, or is there a separate gem or process we should implement?
Since you are using Devise already, you can just overwrite the confirmation controller, try something like this.
class ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation?confirmation_token=abcdef
def show
super do |resource|
YourMailerClass.follow_up(resource).deliver_later(wait_until: 1.day.from_now) if resource.errors.empty?
end
end
end
You also need to update the routes.rb file, add the option controllers: { confirmations: :confirmations } at the end of the line where you define devise_for (restart your server after this).
I'm assuming you already have a background jobs proccesor, like sidekiq
Hope it helps

Unable to route in ruby on rails as I encounter specific error

I'm using rails 5.0 and I get the
PostsController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
NOTE! For XHR/Ajax or API requests while trying to input my url/posts.
my routing.rb file is below:
Rails.application.routes.draw do
get 'posts/index' => 'posts#index'
resources: posts
end
my posts_controller.rb looks like below:
class PostsController < ApplicationController
def index
end
end
I want to know specifically what I'm doing wrong. I've just started to learning Ruby on Rails.
Template is missing. You need to create a file ie index.html.erb under directory /app/views/posts/index.html
GET profiles/employees- Should display all the employees
GET profiles/employees/:id should display a specfic employee
GET profiles/donors - Should display all the donors
GET profiles/donors/:id - Should display a specfic donor
routes in ruby on rails behave in the same way as href tag in rails
we use get for link and if we want toi perform any action on button then w e have to use button.
if you have any doubt let me know in detail.
for more details refer this
http://guides.rubyonrails.org/routing.html
if we write only localhost:3000/employees then bydefault our application will redirect to index page.
The issue is template missing you are missing the template file index.html.erb
I have notice that on your snapshot you named the template file as index.html.erb.txt
remove the .txt and rename to index.html.erb Then it should work
Also you don't need to declare the routes as
get 'posts/index' => 'posts#index'
Just declare the resources: posts is enough. You may check the routes using the rake routes command on your rails console
Rename the file index.html.erb.txt to index.html.erb

Rails gem - Public Activity for earlier posts

The gem does not recognize earlier post which were created before the gem was added.
It only started working, when fresh posts were created. Why was that?
And, how to have those earlier posts get covered by public_activity
Thanks.
Gem setup according to author site.
You've to create the old activities manually using create_activity method. I created a rake task for this.
task public_activity_migration: :environment do
User.find_each do |user|
[:comments, :friends].each do |relation|
user.send(relation).find_each do |obj|
obj.create_activity :create, owner: user, created_at: obj.created_at
print "."
end
end
end
end
The code above will create activities for the comment and friend model. If you're not using strong params you also need to allow the created_at attribute to be set on the PublicActivity::Activity model. This can be done by adding the following code before running your task.
class PublicActivity::Activity
attr_accessible :created_at
end

Riak and Ruby: Ripple not return valid entries

I want use riak with my sinatra application.
I'm using gem ripple, describe simple model and simple form for store data.
Then I want to show all saved entries, using "Article.all" all good, but when i store new entry and refresh page - list of entryies not updates until I restart my application.
I'm trying irb, trying sinatra\reloader, but nothing...
Then, I post new entry, it's momentaly appear in default riak rest interface, and when use riack-client directly all good.
require 'ripple'
require 'sinatra'
class Article
include Ripple::Document
property :title, String
property :body, String
end
get '/' do
#articles = Article.all
erb :articles
end
post '/' do
article = Article.new(:title => params[:title], :body => params[:body])
article.save
end
All is a costly operation in Riak and does not work. If u use the latest version of the Gem for github you will get a error stating the same.

Rails 3.1 asset pipeline outside view

I am trying to set CarrierWave's default url inside of the CarrierWave uploader. To do this, I would like to use the asset pipeline to do the following in uploaders/image_uploader.rb:
def default_url
image_path('question_mark.png')
end
But it fails because: undefined methodimage_path' for :ImageUploader`
Then I tried to add include ActionView::Helpers::AssetTagHelper to uploaders/image_uploader.rb but got this error: undefined local variable or methodconfig' for :ImageUploader`
Any idea how I can get the image_path helper to work outside the view?
I asked a similar question here and concluded there is no way to get any *_path or *_url helpers working in models. I knew that it definitely shouldn't be done (violating MVC and so forth) but it seems it cannot be done at all...
My problem was setting the default_url for a Paperclip attachment and I ended up setting it to the path I would give to image_tag (simply 'image.png' if image.png is located in app/assets/images or public/images) and then using image_path when accessing it. Would that work for you as well?
Not sure exactly what I was trying to do here, ended up working around it but in case anyone comes here wanting to add the _path or _url helpers in their model, do this:
class Post < ActiveRecord :: Base
include Rails.application.routes.url_helpers
# This is so you can use _url params
# you'll need to define the host in your config for it to work though
default_url_options[:host] = Rails.configuration.host
# ...
end
I had a similar problem and I resolved it by doing the following in Rails 3.2.
I declared the following module:
module MyApp::AssetHelper
def config
#config ||= ActionController::Base.config
#config.asset_host = 'http://whereveryouhostyour.assets' #not needed if you have this defined in your environment file
#config
end
def controller
ActionController::Base
end
end
I included the following helpers into the model where I wanted to use the asset_tag helpers
include Sprockets::Helpers::RailsHelper
include MyApp::AssetHelper
This allowed me to call the asset_tag helpers where I needed it.

Resources