0

I'm trying to show info into a dialog. I want such info to be loaded from a view (template). The thing is that, when i use

<%= render template:'panels/product_specifications' %>

it tries to load the info but, as i have global variables used in my view that were defined in the controller, and the controller isn't executed it throws me:

undefined method `banner_image_with_arrows_filename' for nil:NilClass

Extracted source (around line #2):

1: 
2: - banner_image = "banners/panels/#{@panel.banner_image_with_arrows_filename}"
3: #image-info
4:   %table
5:     %tr

@panel variable is defined in controller here:

def load_panel
    @panel = Panel.find_or_build_from_id(params[:id])
    if !@panel.available_in_market?(market)
      add_flash_notice(t("pages.panel.text.not_available_in_market", :market => market.name), true)
    end
end

in a before_filter.

I have also tried with render partial:'panels/products_specifications (having underscored 'product_specifications.html.erb' first), render action:'panels/product_specifications', controller:'product_specifications with different errors.

How can i load that partial or view from another with the controller being executed too?

EDIT:

To give you a big picture, what i'm trying to do is to show the user (when he passes the mouse over a link) a dialog with a preview of what he will see if he click that link.

JGutierrezC
  • 4,398
  • 5
  • 25
  • 42
  • Where are you calling `render_to_string`? In a view? – polarblau Dec 27 '13 at 07:46
  • `render partial:'panels\products_specifications` is that the right path ? It should have been `render partial: 'panels/products_specifications'` – swapab Dec 27 '13 at 08:33
  • @polarblau: sorry, i must have been very sleepy when i post the question. I have corrected ir with only `render` and `render partial: 'panels/products_specifications'` as it is how i did it in my code – JGutierrezC Dec 27 '13 at 15:00

1 Answers1

2

If I understand as well, you hit a action that execute the method load_panel with a before_filter and, the view associated, you're trying to render a template with the variable.

The first thing you should to try is to see if there is a record returned by Panel.find_or_build_from_id(params[:id]). You can use the puts method or use a debugger like pry.

You can also try to do this <%= render template:'panels/product_specifications', panel: @panel %> and use panel as a local variable in your template like this : banners/panels/#{panel.banner_image_with_arrows_filename}.

I think a best practices can be to use a presenter.


Final answer after a chat

You must to use ajax to show a new panel and hit a controller action. You can see an example here.

Dougui
  • 7,142
  • 7
  • 52
  • 87
  • it never gets to the controller. I have put a `debugger` breakpoint and also a `puts "------------------------"` to be printed in the console, but it never gets there. I don't know if `render` doesn't make use of the controller,. i can't use `<%= render template:'panels/product_specifications', panel: @panel %>`, since @panel is not defined in the page i have the links that will show the dialog on click – JGutierrezC Dec 27 '13 at 16:06
  • render will not hit a controller method. You must to use a presenter or create the variable on the controller's action used with the route. – Dougui Dec 27 '13 at 16:16
  • it's not a controller method, is the action. Maybe i'm missunderstanding something. here it is my controller, view and route http://fpaste.org/64549/. What is a presenter? – JGutierrezC Dec 27 '13 at 16:48
  • For presenters, read this http://blog.jayfields.com/2007/03/rails-presenter-pattern.html or watch this http://railscasts.com/episodes/287-presenters-from-scratch. – Dougui Dec 27 '13 at 17:01
  • If I understand, the main problem is than the action `product_specifications` is never triggered. Try to replace your route with this `get '/panels/product_specifications'` (add a `/` at the beggining). – Dougui Dec 27 '13 at 17:18
  • From the panel's index view, i have links to products show views, when the the show view is visited it defines the @panel and other variables on the controller. I exctracted a portion of code from the show view, and created a new view called product_specifications. That 'product_specifications' view i want to show it in a dialog with render method, but as the render doesn't hit the controller, the variables are never set so it throws me an error of nil class. – JGutierrezC Dec 27 '13 at 17:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44006/discussion-between-dougui-and-jgutierrezc) – Dougui Dec 27 '13 at 17:22