How to access a model method from within a view?

I have asked this question on this forum:
http://www.ruby-forum.com/topic/216998#new

I think that main key here is how can I access the method in a model
within a view. Is there a way to do that?

Thanks.

Abder-Rahman A. wrote:

I have asked this question on this forum:
Getting the dimensions of an image element - Rails - Ruby-Forum

I think that main key here is how can I access the method in a model
within a view. Is there a way to do that?

If you have a model object, you can always call methods on it.

But be careful! The principles of MVC architecture state that there
shouldn’t be very much logic (if any!) in your view code. You probably
want to move the method call to a helper or the controller.

Thanks.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks Marnen.

Should I write the code the gets the height and width in a helper?

Do you have an idea of how to do that?

Any ideas on this yet?

models are accessible from all levels of the application, so you can
access
them the same way you access them from controllers

Thanks radhames.

Can you kindly show me a way on how to code this? As I have been stuck
on that for a while.

Thanks.

show the code where you are trying to access, ill see whats wrong with
it

@radhames. If you want me to send you the whole application, can you sen
me your email?

I don’t know if you can send me a private message through
www.ruby-forum.com?

Thanks.

radhames brito wrote:

show the code where you are trying to access, ill see whats wrong with
it

Thanks @radhames.

The main issue is how to make a call to the height() and width() methods
in the model in this line in “show.html.erb”:

This is my model"dicom.rb":

class Dicom < ActiveRecord::Base
has_attached_file :photo, :styles => {
:original => [“100%”, :jpg],
}

def height()
uploaded_file = photo.queued_for_write[:original]
dimensions = Paperclip::Geometry.from_file(uploaded_file)
return dimensions.height
end

def width()
uploaded_file = photo.queued_for_write[:original]
dimensions = Paperclip::Geometry.from_file(uploaded_file)
return dimensions.width
end
end

And, this is my “show.html.erb”:

<%= javascript_include_tag “coordinate” %>

<%= notice %>

Name <%= @dicom.name %>

Image

<%= image_tag @dicom.photo.url , :id => 'dicom_image' %>

<%= update_page_tag do |page| page < <%= update_page_tag do |page| page < <%= link_to 'Edit', edit_dicom_path(@dicom) %> <%= link_to 'Back', dicoms_path %>

Thanks a lot.

Frederick C. wrote:

On Sep 19, 3:11�pm, Abder-Rahman A. [email protected] wrote:

What’s wrong with <%= some_object.width %> ?

Fred

I tried:

But, I don’t the expected dimensions.

As when I calculated the dimensions using this Javascript function:

I found that the image width and height is 512x512, and the displayed
image is not correct, as when I manually enter the height and width
values in the “Canvas”, I get a larger image that the one I’m getting.

On Sep 19, 3:11 pm, Abder-Rahman A. [email protected] wrote:

What’s wrong with <%= some_object.width %> ?

Fred

On 19 September 2010 15:29, Abder-Rahman A. [email protected]
wrote:

Frederick C. wrote:

On Sep 19, 3:11�pm, Abder-Rahman A. [email protected] wrote:

What’s wrong with <%= some_object.width %> ?

Fred

I tried:

You have to put the bits of ruby code that you want expanded inside <%=
%>
<canvas id=“draw” height = <%= @dicom.height %> width= <%= @dicom.width
%> >

Colin

Colin L. wrote:

On 19 September 2010 15:29, Abder-Rahman A. [email protected]
wrote:

Frederick C. wrote:

On Sep 19, 3:11�pm, Abder-Rahman A. [email protected] wrote:

What’s wrong with <%= some_object.width %> ?

Fred

I tried:

You have to put the bits of ruby code that you want expanded inside <%=
%>
<canvas id=“draw” height = <%= @dicom.height %> width= <%= @dicom.width
%> >

Colin

Oh, thansk Colin, I forgot <%= %>. When I do that, I get:

Showing
C:/Users/Abder-Rahman/Desktop/Research/dcm/app/views/dicoms/show.html.erb
where line #2 raised:

is not recognized by the ‘identify’ command.

Either when I write it as:

<canvas id=“draw” height = <%[email protected]%> width= <%[email protected]%> >

Or,

What do you think?

If this is only in the show method - one record, I assume, why not
pass the height and width as instance variables in the method call
which are then available in the view?

Bb Serviss wrote:

If this is only in the show method - one record, I assume, why not
pass the height and width as instance variables in the method call
which are then available in the view?

Thanks @Bb. How do you think this can be done?

" is not recognized by the ‘identify’ command."

you are running the paperclip on windows , you have to hack it so that
the
command can be executed by the cmd.exe

in this branch is suppose to be fixed

ok, im back.

has_attached_file :photo, :styles => { :original => [“100%”, :jpg], }
<=== this here is incomplete and is you are only having the original

image there is no need to specify the style, and paperclip

wont convert the image to jpg, also is not good to put

convertion in the same thread as the main app.

def height()
uploaded_file = photo.queued_for_write[:original]
dimensions = Paperclip::Geometry.from_file(uploaded_file) <====
is
better to save the image in a specific size by doing
return
dimensions.height
:styles=>{:small=>“150x150>”,:big=>“900x900>”}
end

def width()
uploaded_file = photo.queued_for_write[:original]
dimensions = Paperclip::Geometry.from_file(uploaded_file) <====
is
better to save the image in a specific size by doing
return
dimensions.width
:styles=>{:small=>“150x150>”,:big=>“900x900>”}
end
end

them you always know the correct size, but anyway your question is wrong
as
this has nothing to do with models, your height() are unnecesary as you
should just save the width and height on the save event as properties.

add width and hieght as fields of the table.

def before_save
baunds = Paperclip::geometry.from_file(uploaded_file(:original))

width = bounds.width
height = bounds.height

end

then instanciate and object and call the properties from it

Thanks @radhames for your thorough explanation.

I still have the same issue. I think I may have a problem in making an
instantiation.

My model now is as follows:

class Dicom < ActiveRecord::Base
has_attached_file :photo, :styles => {
:original => [“100%”, :jpg],
}

def before_save
uploaded_file = photo.queued_for_write[:original]
dimensions = Paperclip::Geometry.from_file(uploaded_file)
height = dimensions.height
width = dimensions.width
end
end

And, I have done this in “show.html.erb”:

<canvas id=“draw” height="<%[email protected]%>" width="<%[email protected]%>"

What am I missing?

Thanks a lot.

did you created the fields in the database? do that and it should work

radhames brito wrote:

" is not recognized by the ‘identify’ command."

you are running the paperclip on windows , you have to hack it so that
the
command can be executed by the cmd.exe

in this branch is suppose to be fixed

GitHub - ghazel/paperclip: Easy file attachment management for ActiveRecord

I think the link is broken?