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"%>