Forum: Ruby on Rails render yield?!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Heinz S. (Guest)
on 2009-03-04 15:43
Hello,

I've got a little problem with remote functions. I call it for example
like this: <%= link_to_remote 'bla', :url => 'bla/whatever', :method =>
:get %>

that function would be:
def whatever
  flash[:notice] = "You're in whatever!"
  update_content
end

since I don't want the page to be reloaded completely, I update each div
box that needs to be updated:
def update_content
  render :update do |page|
    page.replace 'loginBox', :partial => 'shared/login'
    page.replace 'contentBox', :partial => 'shared/content'
    page.replace 'navigation', :partial => 'shared/navigation'
    page.replace 'menuBox', :partial => 'shared/menu'
  end
end

My problem nos is that the partial views/shared/_content.html.erb has a
yield which doesn't show me the content of views/bla/whatever.html.erb
anymore and only shows it when I access it directly (not using remote
functions).

Does anyone know how I possibly could still show the content of the
template of each action where yield is?

My other approach was:
<%= link_to_remote 'bla', :url => 'bla/whatever', :update =>
'contentBox', :method => :get %>

def whatever
  flash[:notice] = "You're in whatever!"
  respond_to do |format|
    format.html
    format.xml  { head :ok }
  end
end

That would show the template as I want it but doesn't update login,
navigation and menuBox.

Any ideas? I'm kind out of ideas... :(
Heinz S. (Guest)
on 2009-03-04 18:14
I just figured out you can use

yield(page)

def update_content
  render :update do |page|
    page.replace 'loginBox', :partial => 'shared/login'
    page.replace 'contentBox', :partial => 'shared/content'
    page.replace 'navigation', :partial => 'shared/navigation'
    page.replace 'menuBox', :partial => 'shared/menu'
    -----> yield(page) <-----
  end
end

but yield wants some block, if I pass a String in the block nothing
really happens...
Heinz S. (Guest)
on 2009-03-04 19:27
I'm desperate... is there no way to somehow render the default template
AND do some render :update or include the default template in render
:update?
Heinz S. (Guest)
on 2009-03-04 19:58
Even though it became some monolog already...

I found a not so good solution to my problem:
def update_content
  render :update do |page|
    page.replace 'loginBox', :partial => 'shared/login'
    page.replace 'navigation', :partial => 'shared/navigation'
    page.replace 'menuBox', :partial => 'shared/menu'
    page.replace 'contentBody', :file => 'bla/whatever'
  end
end

Is :file a proper solution? If so, is there any variable that tells me
where the call is coming from (in my case it's called whatever) so I
don't have to give the :file path as a parameter whenever I call it?
Peter V. (Guest)
on 2009-03-04 21:50
(Received via mailing list)
On Wed, Mar 4, 2009 at 6:58 PM, Heinz S.
<removed_email_address@domain.invalid> wrote:
> If so, is there any variable that tells me
> where the call is coming from (in my case it's called whatever) so I
> don't have to give the :file path as a parameter whenever I call it?

I am not sure I understand your question.

Would you mean something like

  request.uri etc (that variable is available in the views)

or simply

  controller.action_name (available in the views)

More details for request.uri and (lots of) friends:

  http://api.rubyonrails.org/classes/ActionControlle...

for the controller.action_name:

  http://rubyonrailswin.wordpress.com/2007/03/07/act...

Was that your question ?

HTH,

Peter
Peter V. (Guest)
on 2009-03-04 22:10
(Received via mailing list)
On Wed, Mar 4, 2009 at 8:46 PM, Peter V.
>  request.uri etc (that variable is available in the views)
>
> More details for request.uri and (lots of) friends:
>
>  http://api.rubyonrails.org/classes/ActionControlle...

Sorry, I was mistaken, request.uri does not exist

What does exist is
* request.request_uri
* request.url

The results of a small test:

request.request_uri = /home
request.url = http://localhost:3000/home

These 2 attributes are available both in the controller and in the
views.

HTH,

Peter
Heinz S. (Guest)
on 2009-03-04 22:16
self.controller_name and self.action_name was what I was looking for,
thanks.

Anyone knows an alternative for:
page.replace 'contentBody', :file =>
self.controller_name+'/'+self.action_name?

Looks pretty dirty...
This topic is locked and can not be replied to.