0

Is there a way to make friendly_id dynamic, something like the below, so that depending on an attribute in my user model (next_language) I can adjust how the url's look. I'm using devise. Ideally I would like the controller to handle this logic but can't think of how to do it. The below errors out because the lang model has no idea about current_user.

lang model:

class Lang < ActiveRecord::Base

   if current_user.next_language == "English"
    extend FriendlyId
    friendly_id :english
   end

   if current_user.next_language == "Spanish"
    extend FriendlyId
    friendly_id :spanish
   end

end

here's a sample user record generated when somebody signs up:

User.find(5)
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 5]]
 => #<User id: 5, email: "mtcrts71@gmail.com", encrypted_password: "$2a$10$ZRBXllLt7lmU7YUuCV62.OzlaaleE/XnV4yzQwJtSN9f...", confirmation_token: nil, confirmed_at: "2014-07-19 17:48:29", confirmation_sent_at: "2014-07-19 17:46:15", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-07-19 17:48:29", last_sign_in_at: "2014-07-19 17:48:29", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-07-19 17:46:15", updated_at: "2014-07-19 17:48:29", next_language: "English", admin: false, unconfirmed_email: nil, bookmark: 1, bookmark2: 0, qscore: 0, quiz_flag: 0, total_quiz: 0, natives_language: "Spanish", fqscore: nil>

sample lang record:

Lang.find(1)
  Lang Load (0.8ms)  SELECT "langs".* FROM "langs" WHERE "langs"."id" = $1 LIMIT 1  [["id", 1]]
 => #<Lang id: 1, english: "be", english_to_spanish: "ser", spanish: "una", spanish_to_english: "a", created_at: "2014-07-11 01:30:53", updated_at: "2014-07-11 01:30:53"> 

So based on the above, if the user is learning Spanish the url might look like:

http://localhost:3000/langs/una

or English:

http://localhost:3000/langs/be

Edit to include controller and view code:

controller:

class StaticPagesController < ApplicationController

  def home
    if user_signed_in?
      @placemark = Lang.find(current_user.bookmark)
    end
  end
end

relevant view code:

<%= link_to "Continue Learning!", lang_path(@placemark), class: "btn btn-large btn-primary"%>
mtcrts70
  • 35
  • 4
  • 19

1 Answers1

2

Following the documentation, this should work:

class Lang < ActiveRecord::Base
  extend FriendlyId
  friendly_id :determine_slug

private
  def determine_slug
    case current_user.next_language
    when 'English' then :english
    when 'Spanish' then :spanish
  end

  def current_user
    Thread.current[:current_user]
  end
end

Read how to store the your current_user in Thread.current here: Safety of Thread.current[] usage in rails And be aware of the tradeoffs.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • Did you also add the `current_user` method to `Lang`? – spickermann Jul 20 '14 at 00:54
  • the current_user method is included with Devise so I haven't included it explicitly in Lang. – mtcrts70 Jul 20 '14 at 01:10
  • Device`s `current_user` method is only available in controllers, helpers and views. If you want to use the current user in a model then you have to pass the value into the model. Therefore you need the `current_user` method in your model also and you need to set `Thread.current[:current_user]` like described in the linked answer. – spickermann Jul 20 '14 at 01:17