views:

1893

answers:

7

Hi,

I have a link_to_remote to render the edit action of an object. But all it does is update the Dom Element with this response

try { } catch (e) { alert('RJS error:\n\n' + e.toString()); alert(''); throw e }

My link looks like this:

= link_to_remote t("txt.edit"), :update => dom_id(comment), :url => edit_comment_path(comment.id)

My edit action in the comment controller:

  # GET /comments/1/edit
  def edit
    @comment = Comment.find(params[:id])
    respond_to do |format|
      format.html
      format.js { render :action => "edit" }
    end
  end

The request seems to be ok, according to the log:

Processing CommentsController#edit (for 127.0.0.1 at 2009-04-08 18:55:36) [GET]
  Session ID: 1d4b9b3d3319d5cd556d00d2e053b651
  Parameters: {"authenticity_token"=>"5d70f9e5beded361ee7e87ee591512411e8f3eec", "id"=>"18"}
  User Columns (2.0ms)   SHOW FIELDS FROM `users`
  User Load (0.5ms)   SELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
  Account Columns (1.6ms)   SHOW FIELDS FROM `accounts`
  Account Load (0.2ms)   SELECT * FROM `accounts` WHERE (`accounts`.`subdomain` = 'xxx') LIMIT 1
  Comment Columns (1.7ms)   SHOW FIELDS FROM `comments`
  Comment Load (0.6ms)   SELECT * FROM `comments` WHERE (`comments`.`id` = 18) 
Rendering comments/edit
Completed in 30818ms (View: 2, DB: 7) | 200 OK [http://xx.xxx.rails/comments/18/edit?authenticity_token=5d70f9e5beded361ee7e87ee591512411e8f3eec]

What am I doing wrong? Thanks for the help!

UPDATE: It works by the way with a RJS template - that's how I solved it now. But I still prefer a solution where a view is rendered. Otherwise I have to create a partial just for this purpose (I cannot render Views with rjs page updates - at least I don't know how).

A: 

The one thing you neglected to mention is what the template is... I'm guessing that's the problem. If it's ERB try something like:

format.js { render :action => "edit.html.erb" }

AdminMyServer
I's a haml template, so should I try: render :action => "edit.haml" ?
ole_berlin
Just use the full name of the template. Usually it should be 'action.mime.renderer', so like 'edit.html.haml' should be both the file name and what you send to 'render'
AdminMyServer
A: 

It looks like you may have forgotten to include a reference to the Javascript libraries in your views, and the view is rendering the literal JavaScript instead of interpreting it.

<%= javascript_include_tag :defaults %>
yalestar
Nope, I included them all, otherwise the replacing wouldn't work at all.
ole_berlin
+1  A: 

Or do not catch "respond_to" at all since in both cases You are doing the same! Just comment out:

respond_to do |format|
  format.html
  format.js { render :action => "edit" }
end
aivarsak
That's how I tried it in the beginning, but that didn't work as well...
ole_berlin
+1  A: 
:script => true

Add this to the link_to_remote call and it should make rails evaluate any scripts returned, rather than outputting them plain-text.

Owain Hunt
A: 

When you are responding to a js request, render :action => "edit" will try to render edit.rjs, a rjs 'template', not edit.haml, an html template.

mckeed
A: 

Remove the :update option from your RJS tag. I had one in there and when I was trying to insert_html it was displaying the JS returned from the method and not executing it on the DOM. So it was displaying => try {...}

I took the :update attribute out of my link_to_remote statement in my view, used a render: update do |page| page.insert_html(...) end block and it executed the JS returned instead of displaying it.

Hope that helps.

Greg Hansen
A: 

The problem is the :update => xy statement in your link_to. I had the same problem but i can't tell why it is the :update right now, can't remember ;)

pduersteler