M
M
Meridian3122014-08-19 06:29:13
Ruby on Rails
Meridian312, 2014-08-19 06:29:13

How to make an in-text-tag on a resource in Rails?

Just like in the toaster, you can mention any user by @user_name and then you can follow the link to his profile, you want to do the same for another resource in the application.
I would like to achieve the following, when generating html code with tag markup, the code replaces the tag with route_path (resource), which is available in the controller.
Tell me the solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bukovki, 2014-08-19
@bukovki

I would try to solve the problem using helper. Find a tagged word (for example, @username) with a regular and substitute route_path(resource)
helper:

module PostsHelper
  def in_text_tag(s)
    ...тут действия над s...
  end
end

view: Working example: application_helper.rb:
module ApplicationHelper
  def in_text_tag(s)
    s.sub(/(@\d*)/) {
      |i| User.where(phonenumber: i.gsub(/@/, '')).first.name
    }
  end
end

index.html.erb: The page will generate: Hello, Alekseev Alexey!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question