Show DIV from controller

Hi, I’ve got a basic question but I just can’t find a way or example to
do it.

I have a DIV in my rhtml:

Some text about errors

What I’d like to do is to make it visible from the controller.

I’ve tried things like page[:error_div].show but that complains about
undefined page…

What would be the way to do that simple thing?

Thanks!

Use ‘render :update’ in your controller action:

render :update do |page|
# manipulate page here
end

Stefan L. wrote:

Use ‘render :update’ in your controller action:

render :update do |page|
# manipulate page here
end

Hi, thanks Stefan. I actually tried that and now I’m sure that was the
right direction.

The problem is that when that is executed I get a windows dialog asking
me to download a file. The file contents are:

try { $(“error_div”).show();} catch (e) { alert(‘RJS error:\n\n’ +
e.toString()); alert(’$(“error_div”).show();’); throw e }

I run the browser in RadRails - Aptana IDE and I guess that uses
Explorer. Might be a setting in the browser (?)

Regards

Without seeing your code I am just guessing here, but make sure your
link_to_remote (or other remote call) does not contain the :update =>
some_id option. If it does, the rjs won’t be executed by the browser.

-Bill

Hi,

here’s what I have.

1- A simple view where there’s a form. When I click create button it
will execute the function in the controller. That page also contains the
div “error_div” that I want to make visible if there are some errors.

<%= start_form_tag({ :action => ‘create’ }, :multipart => true) %>
<%= render :partial => ‘form’ %>

<%= submit_tag "Create" %>

<%= end_form_tag %>

Error message

2- The controller will execute the create function. What I do there is
checking some of the fields in the form. If there is a problem with
those I just want to show up the error_div.

def create
if check_parameters_for_new_tab() == -1
puts “returned error -1”
render :update do |page|
page[:error_div].show()
end
# render :action => ‘new’
return
end

(I had some problems with validates_presence_of that’s Why I decided to
check the form myself…)

3- The partial is shown next in case you wonder what it contains…

Mandatory fields

Name
<%= text_field 'tab', 'name' %>

Palo
<%= text_field 'tab', 'palo' %>

Tab Picture
<%= file_field_tag "tab_picture" %>

Tab Audio
<%= file_field_tag "tab_audio" %>

Thanks for checking.

That won’t work because the form is being submitted through a standard
post rather than an xhr call. Take a look at the api docs for
form_remote_tag at
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000535.

-Bill

In your controller, if your object is not valid, just render the action
again. In the view, add an if around the div or set a variable equal to
‘hidden’ or ‘visible’ and use it directly in your style tag. That would
only display the div if there are errors. Slightly dirty, but it will
work.

-Bill

William P. wrote:

That won’t work because the form is being submitted through a standard
post rather than an xhr call. Take a look at the api docs for
form_remote_tag at
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000535.

-Bill

Thanks again Bill. I tried to use form_remote_tag and for a moment I
though it would do it. However I found out that it is not possible to
upload files (multipart is not working). And one of the fields contains
a binary file. So, I’m back at the beginning. There’s some option to use
hidden frames to make uploads with AJAX, but I don’t want to rewrite
everything.

validates_presence_of is able to toggle a div and change its contents.
Any ideas where I can see how that is done there?

Cheers!

Check out the blog post at CodeFU:
http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent

I’ve tried a few plugins to do ajax-like file upload but this simple,
clean approach has been the best so far.

On Nov 25, 4:27 pm, comopasta Gr [email protected]

William P. wrote:

set a variable equal to ‘hidden’ or ‘visible’

Excellent! That’s making the trick perfectly. Thanks!

I’ll check those plugins once I move to a more “AJAXized” version I am
planning.

Cheers!