Topic: Can not get object id in the rjs when using REST

I have a custom action test to try and get rest working with rjs stuff
So far it won't read my id at all and will just parce the code in as text and not the @project.id
But if I hard code the id in it works, so I am missing something here is all I guess ..


#controller
      def hello
      @project = Project.find(params[:project])
           format.html { redirect_to projects_path }
           format.js # rjs ? 
        format.xml { head :ok }
      end

#routes
map.resources :projects, :member => { :close => :post }
  map.resources :projects,
  :colection => { :hello => :get },
  :member => { :hello => :put }

#view
    <td> <%= link_to_remote "Say bite me big_smile", :url => hello_project_path(project), :method => :put %> </td>

#rjs
page.hide 'project_#{@projects.id}'

Re: Can not get object id in the rjs when using REST

you have to use double quotes (" ") instead of single quotes (' ')
ruby expresion parsing in strnigs with #{expression} works only in double quotes.

page.hide "project_#{@project.id}"

(in your posted RJS code, it was @projects.id, i guess that was a typo as it is @project in the controller wink)

Last edited by Duplex (2007-10-18 11:22:40)

Re: Can not get object id in the rjs when using REST

Hmmm, that did not do the trick, I get a huge amasive error instead now as I had been getting in other trys
Also tried this as the controller
      @project = Project.find(params[:id])

Last edited by tripdragon (2007-10-18 11:34:46)

Re: Can not get object id in the rjs when using REST

ok there's something a bit screwed up here. ... first, your routes file..

I realize you took some code i showed you in another post for this, and while i tried to explain about :collection and :member that it's an "either-or" decision, and you won't use both for the same action, you implemented both for the hello action. Maybe i wasn't clear enough on this, my bad.

also, you define the same resource (:projects) twice, which is also wrong.

map.resources :projects,
  :member => { :hello => :put,:close => :post }

Secondly, yes, it should be correct to use @project = Project.find(params[:id])

Can't see any more flaws right now. Please correct the routes file and post back the error message you get if it still doesn't work. "a huge amasive error" is not telling me anything, all Rails errors look kinda huge as it's a page full of output wink

Re: Can not get object id in the rjs when using REST

Thankyou for the help

just to finish up the routes just in case that is the standstill
Are these suppost to be condensed some more ?

 map.resources :projects do |projects|
    projects.resources :iterations
  end
 
  #map.resources :projects, :member => { :close => :post }
  #map.resources :projects, :colection => { :hello => :get }, :member => { :hello => :put }
 
  map.resources :projects, :member => { :hello => :put,:close => :post }

here is the error in the development log
Processing ProjectsController#hello (for 127.0.0.1 at 2007-10-18 11:58:31) [PUT]
  Session ID: 28a04c48e7aacb695816266ac7dae288
  Parameters: {"_method"=>"put", "action"=>"hello", "id"=>"29", "controller"=>"projects"}
Rendering projects/hello


ActionView::TemplateError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id) on line #21 of app/views/projects/hello.rjs:
18: #page.remove dom_id(@project.id)
19:
20:
21: page.hide "project_#{@project.id}"
22:
23:
24: #page.hide "project_#{@projects.id}"


While the error in firebug gives the ammasive error
Firebug is disabled

This page can not be displayed.
POST http://localhost:3000/projects/36;hello500 (642ms)prototype.js (line 866)
ParamsHeadersPostResponse
Response Headers
Request Headers

<html>

<head>

  <title>Action Controller: Exception caught</title>

  <style>

    body { background-color: #fff; color: #333; }

    body, p, ol, ul, td {

      font-family: verdana, arial, helvetica, sans-serif;

      font-size:   13px;

      line-height: 18px;

    }

    pre {

      background-color: #eee;

      padding: 10px;

      font-size: 11px;

    }

    a { color: #000; }

    a:visited { color: #666; }

    a:hover { color: #fff; background-color:#000; }

  </style>

</head>

<body>

<h1>

  RuntimeError in

  Projects#hello

</h1>

<p>

  Showing <i>app/views/projects/hello.rjs</i> where line <b>#21</b> raised:

  <pre><code>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use

object_id</code></pre>

</p>

<p>Extracted source (around line <b>#21</b>):

<pre><code>18: #page.remove dom_id(@project.id)

19:

20:

21: page.hide &quot;project_#{@project.id}&quot;

22:

23:

24: #page.hide &quot;project_#{@projects.id}&quot;

</code></pre></p>

<p></p>

<p><code>RAILS_ROOT: script/../config/..</code></p>

<div id="traces">

     

    <a href="#" onclick="document.getElementById('Framework-Trace').style.display='none';document.getElementById

('Full-Trace').style.display='none';document.getElementById('Application-Trace').style.display='block'

;; return false;">Application Trace</a> |

     

    <a href="#" onclick="document.getElementById('Application-Trace').style.display='none';document.getElementById

('Full-Trace').style.display='none';document.getElementById('Framework-Trace').style.display='block'

;; return false;">Framework Trace</a> |

     

    <a href="#" onclick="document.getElementById('Application-Trace').style.display='none';document.getElementById

('Framework-Trace').style.display='none';document.getElementById('Full-Trace').style.display='block'

;; return false;">Full Trace</a>

 

      <div id="Application-Trace" style="display: block;">

      <pre><code>#{RAILS_ROOT}/app/views/projects/hello.rjs:21:in `__bind_1192723219_440418'

#{RAILS_ROOT}/app/views/projects/hello.rjs:0:in `_run_rjs_47app47views47projects47hello46rjs'</code>

</pre>

    </div>

      <div id="Framework-Trace" style="display: none;">

      <pre><code>/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/object

/extending.rb:44:in `[]'

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/object/extending.rb

:44:in `instance_exec'

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/prototype_helper.rb:313:in

`initialize'


script/server:3</code></pre>

    </div>

      <div id="Full-Trace" style="display: none;">

      <pre><code>#{RAILS_ROOT}/app/views/projects/hello.rjs:21:in `__bind_1192723219_440418'




script/server:3</code></pre>

    </div>

  </div>

<h2 style="margin-top: 30px">Request</h2>

<p><b>Parameters</b>: {&quot;_method&quot;=&gt;&quot;put&quot;,

&quot;id&quot;=&gt;&quot;36&quot;}</p>

<p><a href="#" onclick="document.getElementById('session_dump').style.display='block'; return false;"

>Show session dump</a></p>

<div id="session_dump" style="display:none"><pre class='debug_dump'>---

flash: !map:ActionController::Flash::FlashHash {}

</pre></div>

<h2 style="margin-top: 30px">Response</h2>

<b>Headers</b>: {&quot;cookie&quot;=&gt;[],

&quot;Content-Type&quot;=&gt;&quot;text/javascript&quot;,

&quot;Cache-Control&quot;=&gt;&quot;no-cache&quot;}<br/>

</body>

</html>

Last edited by tripdragon (2007-10-18 12:05:29)

Re: Can not get object id in the rjs when using REST

On further examination I have a feeling that it is not talking to the controller at all, when I rename the method to break it and enter in the full id project_36 it works fine, so I belive something is still missing?
The routes for REST, do they talk to the controller by default? Or is there something else in the "magic" side that is not routed ? I might have got lost in my thought there.

Re: Can not get object id in the rjs when using REST

Routes are still screwed up, but oviously working somehow ... just to clean it up:

#Routes.rb:

map.resources :projects,
              :member => { :hello => :put,:close => :post } do |projects|
    projects.resources :iterations
  end


REST routes call controllers just like normal routes. It's not that much "magic" involved. wink

Judging from error log:
- obviously, @project is nil, therefore, the nil.id error.
- Views never get rendered without callingthe controller. And the eror log says:
"Processing ProjectsController#hello (for 127.0.0.1 at 2007-10-18 11:58:31) [PUT]"
=> I think somehting in the controller is not working, either the project with id 29 doesnt exist, or  there's something else ...

Wow now that i look at your controller again, where's respond_to ?!?!

 def hello
   
   if @project = Project.find(params[:project]) # if/else implemented to see if find fails.
     respond_to do |format| #was missing
       format.html { redirect_to projects_path }
       format.js # rjs ? 
       format.xml { head :ok }
     end # was missing
  else
    raise "Record not found"
  end

end

Re: Can not get object id in the rjs when using REST

I am sorry to say, still nothing changed.
I fixed the routes to your code
altered the controller
restarted the server
checked every file relating to this , *sigh* head hurts now
tested with a full id and that still works,
Even changed the
if @project = Project.find(params[:project]) # if/else implemented to see if find fails.
to  if @project = Project.find(params[:id])


Completed in 0.02958 (33 reqs/sec) | Rendering: 0.01052 (35%) | DB: 0.00345 (11%) | 200 OK [http://localhost/projects]


Processing ProjectsController#hello (for 127.0.0.1 at 2007-10-18 14:07:29) [PUT]
  Session ID: 33834506bf7f3f50641a7aa98a043fe0
  Parameters: {"_method"=>"put", "action"=>"hello", "id"=>"29", "controller"=>"projects"}
Rendering projects/hello


ActionView::TemplateError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id) on line #21 of app/views/projects/hello.rjs:
18: #page.remove dom_id(@project.id)
19:
20:
21: page.visual_effect :highlight, "project_#{@project.id}"
22:
23:
24: #page.hide "project_#{@projects.id}"

Re: Can not get object id in the rjs when using REST

Does any-thing else come to mind that I may have missed ? like maybe link_to_remote behaves in a different way than I need it to ? or it needs some other fangled param ?

I really apreciate the kind help you have given I just am at a lost right now at what this simple example could be.

Wish there was some sort of mini tutorial somewhere for this very subject, it seems I have run into one that is still without a publication yet