I am developing a standalone ruby app in windows platform. How do I connect to MYSOL db. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org lists.rubyonrails.org/mailman/listinfo/rails
In my standard Layout, I have a section which will output an error if one exists. Here is the code: <%unless @message.empty? then%> <%=@message%> <%end%> The problem I''m having sometimes is that the object @message may not have been created in a method that being displayed and I get the error undefined method `empty?'' for nil:NilClass I''m wondering how to elegantly handle this, basically display an error only if the object exists and it''s not empty. I''m sure there is an obvious solution (other than creating a @message object in every method, and I don''t want to use flash) Thanks, Joe _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org lists.rubyonrails.org/mailman/listinfo/rails
Since Nil is a class in Ruby, and has a to_s method (which returns '''') you can use: @message.to_s.empty? Cheers! -DF On 10/12/05, Joseph Lyons <JML-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> > > > In my standard Layout, I have a section which will output an error if one > exists. > > Here is the code: > > <%unless @message.empty? then%> > <%=@message%> > <%end%> > > The problem I''m having sometimes is that the object @message may not have > been created in a method that being displayed and I get the error > > undefined method `empty?'' for nil:NilClass > > I''m wondering how to elegantly handle this, basically display an error only > if the object exists and it''s not empty. I''m sure there is an obvious > solution (other than creating a @message object in every method, and I don''t > want to use flash) > > Thanks, > > > > Joe > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > lists.rubyonrails.org/mailman/listinfo/rails > > >
Ananthakrishnan wrote:> I am developing a standalone ruby app in windows platform. How do I > connect to MYSOL db.That depends if you want to take advantage of ActiveRecord or not (I''d advise it, in general). If so, do this: require ''active_record'' ActiveRecord::Base.establish_connection { :adapter => ''mysql'', :host => ''localhost'', :username => ''username'', :password => ''password'', :database => ''database } and check the documentation for ActiveRecord::Base at api.rubyonrails.com. -- Alex
On 12-okt-2005, at 6:46, Joseph Lyons wrote:> In my standard Layout, I have a section which will output an error > if one exists. > > Here is the code: > > > > <%unless @message.empty? then%><%=@message%><%end%> The problem I’m > having sometimes is that the object @message may not have been > created in a method that being displayed and I get the error > undefined method `empty?'' for nil:NilClass I’m wondering how to > elegantly handle this, basically display an error only if the > object exists and it’s not empty. I’m sure there is an obvious > solution (other than creating a @message object in every method, > and I don’t want to use flash) Thanks,<%= @message unless @message.blank? %> -- Julian "Julik" Tarkhanov
If you aren''t using activerecord, you can get the ruby-mysql bindings and do something like: require ''mysql'' db = Mysql.new("localhost","username","password") db.select_db("database") and execute queries with db.query(sql), etc: tmtm.org/en/mysql/ruby On Wed, 2005-10-12 at 10:38 +0100, Alex Young wrote:> Ananthakrishnan wrote: > > I am developing a standalone ruby app in windows platform. How do I > > connect to MYSOL db. > That depends if you want to take advantage of ActiveRecord or not (I''d > advise it, in general). If so, do this: > > require ''active_record'' > > ActiveRecord::Base.establish_connection { > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''username'', > :password => ''password'', > :database => ''database > } > > and check the documentation for ActiveRecord::Base at > api.rubyonrails.com. >
you could use: <%=@message unless not @message %> Which will print an empty string if @message is an empty string. If you don''t want to output anything if @message is empty then: <%if @message && not @message.empty? then%> <%=@message%> <%end%> but I''m fairly new at this and there may be a more *Rubyish* way Tom Joseph Lyons wrote:>In my standard Layout, I have a section which will output an error if one >exists. > >Here is the code: > > > ><%unless @message.empty? then%> ><%=@message%> ><%end%> > >The problem I''m having sometimes is that the object @message may not have >been created in a method that being displayed and I get the error > >undefined method `empty?'' for nil:NilClass > >I''m wondering how to elegantly handle this, basically display an error only >if the object exists and it''s not empty. I''m sure there is an obvious >solution (other than creating a @message object in every method, and I don''t >want to use flash) > >Thanks, > > >Joe > > > > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >lists.rubyonrails.org/mailman/listinfo/rails > > >------------------------------------------------------------------------ > >No virus found in this incoming message. >Checked by AVG Anti-Virus. >Version: 7.0.344 / Virus Database: 267.11.14/131 - Release Date: 12/10/2005 > > >
You could do: <%= @message rescue nil %> And it will output the message if there is one. If the message is nil nothing will happen, it won''t throw and error and it will continue on with you script. Cheers- -Ezra On Oct 13, 2005, at 5:06 AM, Tom Ayerst wrote:> you could use: > > <%=@message unless not @message %> > > Which will print an empty string if @message is an empty string. If > you don''t want to output anything if @message is empty then: > > <%if @message && not @message.empty? then%> > <%=@message%> > <%end%> > > but I''m fairly new at this and there may be a more *Rubyish* way > > Tom > > Joseph Lyons wrote: > > >> In my standard Layout, I have a section which will output an error >> if one >> exists. >> >> Here is the code: >> >> >> <%unless @message.empty? then%> >> <%=@message%> >> <%end%> >> The problem I''m having sometimes is that the object @message may >> not have >> been created in a method that being displayed and I get the error >> undefined method `empty?'' for nil:NilClass >> I''m wondering how to elegantly handle this, basically display an >> error only >> if the object exists and it''s not empty. I''m sure there is an >> obvious >> solution (other than creating a @message object in every method, >> and I don''t >> want to use flash) >> Thanks, >> >> Joe >> >> >> >> --------------------------------------------------------------------- >> --- >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> lists.rubyonrails.org/mailman/listinfo/rails >> >> --------------------------------------------------------------------- >> --- >> >> No virus found in this incoming message. >> Checked by AVG Anti-Virus. >> Version: 7.0.344 / Virus Database: 267.11.14/131 - Release Date: >> 12/10/2005 >> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > lists.rubyonrails.org/mailman/listinfo/rails > >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
(Apologies in advance for a rather drawn-out question...) This is more of a Ruby question than a Rails question, but I can''t see the answer anywhere. Given an expression like: @item = ''foo'' how can I get the @item object''s local symbol as a string(in this case, ''item''? A little context may be in order, as I''m possibly coming at this from the wrong angle. The reason I''m doing this is for a helper. It always struck me that, by having a form''s action defined in a view, we are distributing business logic into the views. Surely an action should know what the consequences are likely to be? In the Rails model, I tend to end up with a lot of single-form pages that come from one action and go directly to another. I''ve knocked together a quick helper to try to address this, which lets you do (for example): class ItemController < ApplicationController def new @item = Item.new @user = User.new targets :create end def edit @item = Item.find(params[:id]) @user = User.find(params[:user][:id]) targets :update, with [@item,@user] end def create ... end def update ... end end In the view (say, for the ''edit'' action), I then do: <h1>Editing item</h1> <%= render_single_form(''item_form'') %> And it''s the same in the ''new'' action: <h1>Creating new item</h1> <%= render_single_form(''item_form'') %> The render_single_form method in the helper looks like this: def render_single_form(partial) [form_tag @form_target, render_embedded, render(:partial => partial, get_locals), end_form_tag].join("\n") end My problem is with the get_locals() method. Partials need the locals passed in as a hash of internal_name => value. Given that I''ve already got a list of embedded objects from my with() method, I''d rather re-use that than repeat the information in the render_single_form() call, or pass it in another way. Any bright ideas? Have I missed something obvious? -- Alex
On 10/22/05, Alex Young <alex-qV/boFbD8Meu8LGVeLuP/g@public.gmane.org> wrote:> (Apologies in advance for a rather drawn-out question...) > > This is more of a Ruby question than a Rails question, but I can''t see > the answer anywhere. > > Given an expression like: > > @item = ''foo'' > > how can I get the @item object''s local symbol as a string(in this case, > ''item''? A little context may be in order, as I''m possibly coming at > this from the wrong angle.I don''t think you can do something like name(@item) #=> "item", because passing is done by reference (you''d always be passing in the object that @item referred to). You can, however, get a list of instance variables using the instance_variables method: irb(main):001:0> class A irb(main):002:1> def initialize irb(main):003:2> @foo = "bar" irb(main):004:2> @baz = "quux" irb(main):005:2> @wossname = "thingy" irb(main):006:2> end irb(main):007:1> end => nil irb(main):008:0> a = A.new => #<A:0xb7c37bf0 @wossname="thingy", @baz="quux", @foo="bar"> irb(main):009:0> a.instance_variables => ["@wossname", "@baz", "@foo"] martin