0

i have been working on a specific use case, where we need to render something like

render body: nil
render nothing: true

so these two things has to used in different methods, like

def render_method
  if some_condition
    render body: nil
  else 
    render nothing: true
  end
end

so while calling this, from another method like

def method2
  render_method
end 

Anyhow this is rendering to the method 2.

Is there any way for this to render to the method which called above?

Holger Just
  • 52,918
  • 14
  • 115
  • 123
jansha
  • 164
  • 1
  • 8
  • 1
    "anyhow this is rendering to the method 2" - what does this mean exactly, "render to a method"? – Sergio Tulentsev Feb 22 '23 at 13:52
  • @SergioTulentsev i mean the render_method, from where the second method gets called. – jansha Feb 22 '23 at 13:56
  • 2
    I still have no idea what you expect to happen and what happens instead. – Sergio Tulentsev Feb 22 '23 at 13:57
  • 1
    It is perfectly fine to call a method that renders differently depending on a condition. And I would basically work like the methods in your question, with the only difference that both, `body: nil` and `nothing: true` or invalid parameters to the [`render`](https://api.rubyonrails.org/classes/ActionController/Renderer.html#method-i-render) method. When you elaborate on what you want to achieve and what should be returned in both cases, then we might be able to help. – spickermann Feb 22 '23 at 14:11
  • RE `nothing: true`: it used to work. https://stackoverflow.com/questions/43428991/what-to-use-instead-of-render-text-and-render-nothing-true-in-rails-5-1 – Sergio Tulentsev Feb 22 '23 at 14:25

1 Answers1

0

I'm not sure to fully understand your question but you have to add return if you are in this case:

def index
  if condition
    render html: 'HTML HERE' and return
  end 

  render json: {}
end
Alexis CHEVREUX
  • 132
  • 1
  • 11