Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@mmusasabi

【メモ】modelからURLのパスを取得したい。【Rails tips】

More than 5 years have passed since last update.

概要

RailsのモデルでURLを取得したい。だからrequestオブジェクトを使いたい。
でも、既に実装したメソッドの拡張で、使用範囲も広い。ので引数を与えるのは良くない。

解決

そんな時に出会った記事がこちら。

railsでrequest変数をmodelで使う方法

before_filter :set_request_filter

  def set_request_filter
     Thread.current[:request] = request
  end  
request = Thread.current[:request]

アプリケーションコントローラーのbeforefillterで毎回変数に入れてあげることで
modelで受け取ることが出来るという感じです!

...でもコレだと使わない部分でもいちいち変数入れちゃうような動作になるなぁ。

もっときれいなやり方ないかな。

募集中です><

2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
mmusasabi
(*´д`*)パッション!!

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?