Home>

We are developing with front React backend Rails.

I was able to post an image to active storage with axios using multipart/form-data and save it, but I don't know how to get it with get, so I have a question.

def index
    articles = Article.all
    render json: articles
end
const Top = () =>{
  const [articles, setArticles] = useState ([]);
  useEffect (() =>{
    const getArticles = async () =>{
      try {
        const results = await json.get ('/ articles')
        setArticles (results.data)
      } catch (error) {
        console.log (error)
      }
    }
    getArticles ()
  }, [])
  return (
  <>
      
  </>
  )
}

I want to pass the image data with the index of cnotroller in this get request.
Thanks for your cooperation.

  • Answer # 1

    def index
    articles = Article.all.with_attached_image
    render json: articles.map {
    | article |
    article.as_json.merge ({{
    image: url_for (article.image)
    })
    }
    end
    I was able to do this.

Related articles