Coder Social home page Coder Social logo

Comments (10)

nesquena avatar nesquena commented on August 19, 2024

I never considered using a hash as the object, interesting idea. I always used a class (ORM) record as the object. I guess I don't see a reason a Hash couldn't be made to work but not surprising it doesn't. There are a few assumptions I made about the object right now and one of them is that the object responds to the "valid?" method and that it has dot notations to access attributes.

Here's how I would achieve what you want for now:

object false
node(:foo) { "bar" }
node(:pop) { 123 }

or if its a nested hash:

object false
node(:thing) { @thing }

I have to give this some thought, this is a use case I never considered supporting.

from rabl.

nesquena avatar nesquena commented on August 19, 2024

Also as for the undefined methodto_html' for #Rabl::Engine:0x1075c0730` it looks like you are trying to use RABL during an HTML request (not a json or xml request). You probably shoudn't allow the action to respond to HTML or have an alternate erb or haml template for that case...

from rabl.

kimptoc avatar kimptoc commented on August 19, 2024

Hi,

Thanks for the feedback - will try what you suggest.

I tried creating a local object like this:

class Thing
    attr_accessor :foo, :pop
    def initialize(a,b)
      @foo =a
      @pop =b
    end

  end

So the controller did this:

@thing2 = Thing.new("boo",987)

And template is this:

object @thing2
attributes :foo, :pop

Getting 'null' response (when I specify the format).

On the to_html stuff, yesterday I got away without it - not sure how. Wonder if there is a way to make json the default format.

Thanks,
Chris

from rabl.

kimptoc avatar kimptoc commented on August 19, 2024

Hi,

Just to say your suggestion with the "object false" does the job - and is clearer in my controller, so I prefer it - the hash was a contrived object to get the data to the view - so better to not have it :)

Thanks,
Chris

from rabl.

nesquena avatar nesquena commented on August 19, 2024

@kimptoc the to_html error comes from this change yesterday: ed9ec55 in which I detect the request format if theres no explicit format. I will have to fix it by disallowing html as the format I think and default to json.

As for the Thing record, try this for fun:

class Thing < Struct.new(:foo, :pop)
   def initialize(h)
     super *h.values_at(*self.class.members.map {|s| s.intern})
   end

  def valid?; true; end
end

and

@thing2 = Thing.new(:foo => "boo", :pop => 987)

Can you tell me if that works?

from rabl.

nesquena avatar nesquena commented on August 19, 2024

@kimptoc Okay, I am actually going to close this for now, I have never had the need to use a hash as the object. I don't think I will in the future either. In the case of constructing a response from scratch I would go with the object false approach :) If you or someone else finds this issue, I am not against the idea if someone else would like to patch it in with a pull request. I will fix the "to_html" issue tho soon. Thanks!

from rabl.

kimptoc avatar kimptoc commented on August 19, 2024

Hi,

The Thing suggestion works just fine and yes, please close it - was going to suggest that.

Regards,
Chris

from rabl.

nesquena avatar nesquena commented on August 19, 2024

@kimptoc Fixed the 'to_html' issue here, can you confirm? 7f97b7e

from rabl.

kimptoc avatar kimptoc commented on August 19, 2024

Hey @nesquena - perfect, works like a charm :)

Many thanks.

from rabl.

nesquena avatar nesquena commented on August 19, 2024

Excellent thanks man, pushing out a small gem point release.

from rabl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.