I''m new to MVC programming and I''d appreciate some help with this query. I have a ''image'' model which my ''page'' model uses to get the html <img> tag. The image model contains a filename and width/height info which it wraps into an <img> tag and returns to the caller, but where in the MVC pattern does this code belong? As I understand it, the view is responsible for rendering output, the model handles reading/writing data and the controller sits in between and handles users requests. Somewhere in my page controller I need to say something like : image = img.get_html() is it wrong to put the get_html() method in the model? thanks dylan
Hello Dylan!> I''m new to MVC programming and I''d appreciate some help with this query. > I have a ''image'' model which my ''page'' model uses to get the html <img> > tag. The image model contains a filename and width/height info which it > wraps into an <img> tag and returns to the caller, but where in the MVC > pattern does this code belong?The controller gets the request from the user, asks the model for the data and forwards it to the view, which constructs the HTML (simplified). So you write in the controller: @image = Image.find(1) and in the view something like: <img width="<%= @image.width %>" height="<%= @image.height %>" src="<%@image.filename %>"> Michael
Michael Raidel wrote:> Hello Dylan! > > >>I''m new to MVC programming and I''d appreciate some help with this query.. >>I have a ''image'' model which my ''page'' model uses to get the html <img> >>tag. The image model contains a filename and width/height info which it >>wraps into an <img> tag and returns to the caller, but where in the MVC >>pattern does this code belong? > > > The controller gets the request from the user, asks the model for the data > and forwards it to the view, which constructs the HTML (simplified). So > you write in the controller: > > @image = Image.find(1) > > and in the view something like: > <img width="<%= @image.width %>" height="<%= @image.height %>" src="<%> @image.filename %>"> > > > Michael >This is generally the way I do it, though I wrap up the whole thing in a helper to avoid strenuous repetition. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Another way, and one that''s served me well, is to use a partials to generate the img tag. The added benefit is that there would be enough flexibility to implement something like mouse-over image changing without changing the code that renders the partial. I have an images controller and it has actions tiny, medium, large for the three thumbnail sizes it serves up, and it has a partial for tiny and medium that right now just generates the image tag pointing to the corresponding action. That way if I have items which has_one image, I can write: <%=render_partial ''images/tiny'', @part.image %> when I want to include it in a page. Brian On Mon, 07 Feb 2005 08:12:01 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote:> Michael Raidel wrote: > > Hello Dylan! > > > > > >>I''m new to MVC programming and I''d appreciate some help with this query.. > >>I have a ''image'' model which my ''page'' model uses to get the html <img> > >>tag. The image model contains a filename and width/height info which it > >>wraps into an <img> tag and returns to the caller, but where in the MVC > >>pattern does this code belong? > > > > > > The controller gets the request from the user, asks the model for the data > > and forwards it to the view, which constructs the HTML (simplified). So > > you write in the controller: > > > > @image = Image.find(1) > > > > and in the view something like: > > <img width="<%= @image.width %>" height="<%= @image.height %>" src="<%> > @image.filename %>"> > > > > > > Michael > > > > This is generally the way I do it, though I wrap up the whole thing in a > helper to avoid strenuous repetition. > > -Scott > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
On 07 Feb 2005, at 07:29, Brian L. wrote:> On Mon, 07 Feb 2005 08:12:01 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> > wrote: >> Michael Raidel wrote: >>> Hello Dylan! >>> >>>> I''m new to MVC programming and I''d appreciate some help with this >>>> query.. >>>> I have a ''image'' model which my ''page'' model uses to get the html >>>> <img> >>>> tag. The image model contains a filename and width/height info >>>> which it >>>> wraps into an <img> tag and returns to the caller, but where in the >>>> MVC >>>> pattern does this code belong? >>> >>> @image = Image.find(1) >>> >>> and in the view something like: >>> <img width="<%= @image.width %>" height="<%= @image.height %>" >>> src="<%>>> @image.filename %>"> >> >> This is generally the way I do it, though I wrap up the whole thing >> in a >> helper to avoid strenuous repetition. > > <%=render_partial ''images/tiny'', @part.image %> > > when I want to include it in a page.I don''t like all that typing because it is too much to remember, so I just shove it right into the Model. <%= @image.tag :small %> (If you feel bad about the MVC-ness of this, put it in a module, and include it into your model.) -- Eric Hodel - drbrain-48TerJ1FxhPk1uMJSBkQmQ@public.gmane.org - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Eric Hodel wrote:> I don''t like all that typing because it is too much to remember, so I > just shove it right into the Model. > > <%= @image.tag :small %> > > (If you feel bad about the MVC-ness of this, put it in a module, and > include it into your model.)Seconded! I really like using modules to selectively include view helper methods in models. It may be ''broken'', but it works great :) jeremy
Jeremy Kemper wrote:> Eric Hodel wrote: > >>I don''t like all that typing because it is too much to remember, so I >>just shove it right into the Model. >> >><%= @image.tag :small %> >> >>(If you feel bad about the MVC-ness of this, put it in a module, and >>include it into your model.) > > > Seconded! I really like using modules to selectively include view > helper methods in models. It may be ''broken'', but it works great :) > jeremy > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >some great responses there gents, thanks. I should''ve been clearer with my original post. My problem is the page object has no idea what kind of object it needs to render. it could be an image, a paragraph, a forum topic etc. all it knows is that it has a collection of child objects and it needs to query each one to get their html content. (something like object.get_html() in a for loop) I like the idea of modules though and I think I''ll include a function in the module which id''s the object, calls a partial or helper specific to that object then returns the html thanks for your help. this is a great list! dylan
Dylan wrote:> Jeremy Kemper wrote: > >> Eric Hodel wrote: >> >>> I don''t like all that typing because it is too much to remember, so I >>> just shove it right into the Model. >>> >>> <%= @image.tag :small %> >>> >>> (If you feel bad about the MVC-ness of this, put it in a module, and >>> include it into your model.) >> >> >> >> Seconded! I really like using modules to selectively include view >> helper methods in models. It may be ''broken'', but it works great :) >> jeremy >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > some great responses there gents, thanks. > > I should''ve been clearer with my original post. My problem is the page > object has no idea what kind of object it needs to render. it could be > an image, a paragraph, a forum topic etc. all it knows is that it has a > collection of child objects and it needs to query each one to get their > html content. (something like object.get_html() in a for loop) > > I like the idea of modules though and I think I''ll include a function in > the module which id''s the object, calls a partial or helper specific to > that object then returns the html > > thanks for your help. > this is a great list!You could also use the Inflector to get an underscored_name for the ModelClass and then use the partials way like <%= render_partial ''shared/#{underscored_name}'', @item %> -- Marten Veldthuis
IMHO, having that much coupling (the model is aware of the URL that is accessing it) is probably a bad design decision. I see the convenience, but a few characters difference is not worth sacrificing MVC like that. Even with a module included in the model, you still have the model manipulating the view/controller which is backwards. If you wanted to do this in a similar way, at least put it in the application helper: <%=tiny_image_tag @image %> or <%= image_tag @image, :tiny %> where image_tag/tiny_image_tag pulls the id out of image and makes an IMG tag for it. I think both of these are as expressive and convenient as putting it in the model, except they won''t create the same kind of headaches down the road. Brian On Mon, 07 Feb 2005 09:51:38 -0800, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> Eric Hodel wrote: > > I don''t like all that typing because it is too much to remember, so I > > just shove it right into the Model. > > > > <%= @image.tag :small %> > > > > (If you feel bad about the MVC-ness of this, put it in a module, and > > include it into your model.) > > Seconded! I really like using modules to selectively include view > helper methods in models. It may be ''broken'', but it works great :) > jeremy > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)