I cannot find a complete example of this on the web. Here''s what I''m trying to do: In a view I have a remote_function call that right now looks like this: <% @artists.each do |artist| %> <li class="link" onclick="<%= remote_function(:update => "album_box", :url => { :action => :list_albums }, :with => "id=#{artist.id}") %>"> <%=artist.name%></li> <%end%> and in the corresponding controller: def list_albums @albums=Album.find(params[:id]) @albums=@albums.sort { |a,b| a.title <=> b.title } end Either or both of these is wrong, since in the end I get a "Can''t find an album without id". There is a complicating issue, too, since if I change the parameter name, I get "undefined local variable". Anyone got a quick example of the syntax for what *must* be a very common and simple task??? -- Posted via http://www.ruby-forum.com/.
First off, have you ''thrown'' the params hash that you get in the controller? You can paste it back here if this did not help you solve your problem. -Mike On May 23, 12:01 pm, Mk 27 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I cannot find a complete example of this on the web. > > Here''s what I''m trying to do: In a view I have a remote_function call > that right now looks like this: > > <% @artists.each do |artist| %> > <li class="link" onclick="<%= remote_function(:update => "album_box", > :url => { :action => :list_albums }, :with => "id=#{artist.id}") %>"> > <%=artist.name%></li> > <%end%> > > and in the corresponding controller: > > def list_albums > @albums=Album.find(params[:id]) > @albu...-XoHWyCL3peZHwiGOKpAqag@public.gmane.org { |a,b| a.title <=> b.title } > end > > Either or both of these is wrong, since in the end I get a "Can''t find > an album without id". There is a complicating issue, too, since if I > change the parameter name, I get "undefined local variable". > > Anyone got a quick example of the syntax for what *must* be a very > common and simple task??? > -- > Posted viahttp://www.ruby-forum.com/.
Mike Rose wrote:> First off, have you ''thrown'' the params hash that you get in the > controller? > > You can paste it back here if this did not help you solve your > problem. > > -MikeWhere, like "out the window"? ;) I do not understand what you mean by thrown. Here''s something (else) strange (to me): I am trying to work this out now with link_remote, since that''s simpler until I get the nitty gritty. <li><%=link_to artist.name, :action => "list_albums", :find_id => artist.id %></li> I notice that WEBbrick output sez: Parameters: {"artist_id"=>"65"} Shouldn''t this be passed as "find_id"???? I guess I could have this backward, will test. Maybe I''ll also try to log output to a file in the controller, if rails will allow me to debug that way... Anyway, really the best thing for me would be an example paralleling the OP request. -- Posted via http://www.ruby-forum.com/.
Okay, I got the WEBbrick output to agree with me. Here''s the line in the controller now, which is still wrong: @albums=Album.find(:all, :conditions => [:artist_id => params[:find_id]]) NoMethodError in MainController#list_albums undefined method `%'' for {:artist_id=>"67"}:Hash -- Posted via http://www.ruby-forum.com/.
Well, to be honest your first post looks correct by inspection. If you are just posting the id though you do not necessarily need to use :with. You can group all the items you want within the :url block. Also, by saying ''throw'', I mean that in your action in the controller have ''throw params'' as the first line and then when you try to access that action it will output, to the screen, a trace that shows you what parameters are coming across from the view. Very helpful. You can keep throwing until you get the correct params coming across and then focus on fixing or writing your action code. On May 23, 12:28 pm, Mk 27 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Mike Rose wrote: > > First off, have you ''thrown'' the params hash that you get in the > > controller? > > > You can paste it back here if this did not help you solve your > > problem. > > > -Mike > > Where, like "out the window"? ;) I do not understand what you mean by > thrown. > > Here''s something (else) strange (to me): I am trying to work this out > now with link_remote, since that''s simpler until I get the nitty gritty. > > <li><%=link_to artist.name, :action => "list_albums", :find_id => > artist.id %></li> > > I notice that WEBbrick output sez: > > Parameters: {"artist_id"=>"65"} > > Shouldn''t this be passed as "find_id"???? I guess I could have this > backward, will test. Maybe I''ll also try to log output to a file in the > controller, if rails will allow me to debug that way... > > Anyway, really the best thing for me would be an example paralleling the > OP request. > -- > Posted viahttp://www.ruby-forum.com/.
> @albums=Album.find(:all, :conditions => [:artist_id => > params[:find_id]])Good practice for it to read like this @albums = Album.find(:all, :conditions => "where artist_id = ''#{params [:find_id}''") On May 23, 12:34 pm, Mk 27 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Okay, I got the WEBbrick output to agree with me. Here''s the line in > the controller now, which is still wrong: > > @albums=Album.find(:all, :conditions => [:artist_id => > params[:find_id]]) > > NoMethodError in MainController#list_albums > undefined method `%'' for {:artist_id=>"67"}:Hash > > -- > Posted viahttp://www.ruby-forum.com/.
Hmmm, I seem to be uncovering a more basic layer to my miscomprehension. Thanks people, I think your input will come in handy, but bear with me for a minute: Here''s where I got to in trying to debug (just noticed stdout in the controller is the server stdout, handy). Hence its long winded style: [in controller method] all=Album.find(:all) @albums = Array.new tmp=params[:find_id] all.each { |rec| if rec.artist_id == tmp @albums.push rec else puts "->#{rec.artist_id} != #{tmp}" end } Now, what I find mind boggling is some of the output: ->67 != 67 ->67 != 67 ->67 != 67 ->67 != 67 ->67 != 67 ->67 != 67 ->67 != 67 WHAT THE HECK? I''m new to ruby, this seems an odd feature...I would guess this is about data types??!!? Both numbers are sourced from an sqlite INTEGER. -- Posted via http://www.ruby-forum.com/.
On Sat, May 23, 2009 at 1:01 PM, Mk 27 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> ->67 != 67 > > WHAT THE HECK? I''m new to ruby, this seems an odd feature...I would > guess this is about data types??!!? Both numbers are sourced from an > sqlite INTEGER.Nope, nothing to do with Ruby -- HTTP request parameters are *always* strings. If you need to compare to an integer, use params[:whatever].to_i -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Hassan Schroeder wrote:> Nope, nothing to do with Ruby -- HTTP request parameters are > *always* strings.That makes sense. Thanks much everyone! -- Posted via http://www.ruby-forum.com/.
Furthermore, naming conventions in rails/ruby say you should not have a variable called tmp, temp, temporary, or any of that ambiguous naming. Also, now that you know you should be casting to_i from params, try using the original method I suggested @albums = Album.find(:all, :conditions => "where artist_id = ''#{params [:find_id].to_i}''") and let me know if that solved the issue. It''s really bad/wasteful to pull down all of your artists. Also, shorthand @collection = Model.find(:all) is the same as @collection = Model.all likewise, you can do Model.first and Model.last to get the first and low entry of a table! Good luck. On May 23, 1:24 pm, Mk 27 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hassan Schroeder wrote: > > Nope, nothing to do with Ruby -- HTTP request parameters are > > *always* strings. > > That makes sense. > > Thanks much everyone! > > -- > Posted viahttp://www.ruby-forum.com/.
Mike Rose wrote:> and let me know if that solved the issue. It''s really bad/wasteful to > pull down all of your artists. Also, shorthandYeah, that is what I meant by "long winded". *Why is there this issue with the name of a local, temporary variable?* If the scope is only a few lines, I might as well call it "x" (which is why I think tmp is *unambiguous*: it indicates this variable is just that, and nothing more than that)...Is this just the normal "pick informative names" caveat, or is there more to it? Rails is slightly scary... Your "where =" line did not work -- I may have mistyped it, but this seems to do: @albums = Album.find(:all, :conditions => { :artist_id => params[:find_id].to_i }) Most of all: Your help is much appreciated dude, THANKS! ps. If you have a minute, here''s a little question about what I am about to try: I have a list on one side with all the artists. A click on a list entry will put all the artist''s albums in a div box on the other side. Now, I want to add a menu up top with entries like "Sort albums by size", but I want that to apply only to the albums that are currently displayed. I don''t want to produce the list again and sort it (since I have some more complicated plans, like allowing the user to build a custom list in the div box); I want to try and keep a global variable ($shown) and process that back in the album controller. Hopefully this is possible -- any related tips brought to mind? -- Posted via http://www.ruby-forum.com/.
Mike Rose wrote:>> @albums=Album.find(:all, :conditions => [:artist_id => >> params[:find_id]]) > > Good practice for it to read like this > > @albums = Album.find(:all, :conditions => "where artist_id = ''#{params > [:find_id}''")Nope. Your "good practice" is in fact more typing and more literal SQL for what as far as I can see is no good reason at all. Take advantage of Rails'' syntactic sugar and stick with the first version. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Note: remove the where in the string, I was typing a bit too quickly and that does not belong. I should have said my preferred practice as well. But glad everything worked out. On May 23, 5:06 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote:> Mike Rose wrote: > >> @albums=Album.find(:all, :conditions => [:artist_id => > >> params[:find_id]]) > > > Good practice for it to read like this > > > @albums = Album.find(:all, :conditions => "where artist_id = ''#{params > > [:find_id}''") > > Nope. Your "good practice" is in fact more typing and more literal SQL > for what as far as I can see is no good reason at all. Take advantage > of Rails'' syntactic sugar and stick with the first version. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.