My model has 2 attributes, image_uri and title. image_uri is defined as a blob attribute in image.rb. The intention is to allow the user to enter a title for the image and then to choose or take a picture and then store the same in the model.
Initially, there is no record, so on clicking "New" button on the index page, the user is taken to new.erb :
<div data-role="page">
<div data-role="header" data-position="inline">
<h1>New <%= @image.title %></h1>
<a href="<%= url_for :action => :index %>" class="ui-btn-left" data-icon="back" data-direction="reverse">
Cancel
</a>
</div>
<div data-role="content">
<form method="POST" action="<%= url_for :action => :create %>">
<input type="hidden" name="id" value="<%= @image.object %>"/>
<div data-role="fieldcontain">
<label for="image[title]" class="fieldLabel">Title</label>
<input type="text" id="image[title]" name="image[title]" <%= placeholder("Title") %> />
</div>
<div data-role="fieldcontain">
<label for="image[image_uri]" class="fieldLabel">Image uri</label>
<input type="text" id="image[image_uri]" name="image[image_uri]" <%= placeholder("Image URI") %> />
</div>
<input type="submit" value="Create"/>
</form>
</div>
</div>
Here's my create method in controller as well as the callback :
# POST /Image/create
def create
Camera::choose_picture(url_for :action => :camera_callback)
""
end
def camera_callback
if @params['status'] == 'ok'
@image=Image.new({'image_uri' => @params['image_uri'],'title' => @params['title']})
@image.save
end
WebView.navigate( url_for :action => :index )
""
end
The problem is that the record is created, but only image_uri is saved, whereas title is not.
It looks like when camera_callback is called, @params['title'] is lost.
So, how do I pass in the value of title to the callback?
Or is there another/better way?
I'm sure this is a piece of cake to many of you folks!
TIA
Yes, I tried that,even this (2 colons before get_blob_path as per documentation) :
<img src="<%= Rho::RhoApplication::get_blob_path(@image.image_uri) %>" />
but its giving "Error Loading Page" in Rhosimulator, so I doubt if it will work in device.
What's more puzzling is that I have the sample app from an old webinar, where I don't have to use Rho::RhoApplication::get_blob_path as simply doing src="<%=@image.image_uri%>" works even in device (tested in both WM & Android). Not sure if its because of layout.erb or not using jQM or what.
I can send that working webinar app if you are interested.
Points: 0
You voted ‘up’
Hi Mark,
Can you try the below snippet ?
Check your params value on create method then pass that value to callback method using query
Visnupriya R
Kutir Mobility
Points: 0
You voted ‘up’
Hi Visnupriya,
Thanks for the response. I tried with your code and modified my callback thus :
@image=Image.new({'image_uri' => @params['image_uri'],'title' => @image_params['title']})
@image.save
But it didn't work. On clicking Create on the view, it allows selection of picture, picture selected, and then I'm shown an "Undefined" page. I read in the docs (http://docs.rhomobile.com/rhodesapi/camera-api) that the message in the camera callback is a POST message and contains only a few parameters, so maybe it cannot "carry along" the query as you suggested.
So, I did it this way (this is not the most elegant solution, but it works as long as user selects a pic from the dialog) :
I make image_uri as readonly field in all ERBs, except in show.erb, where I display the image using img tag.
This is my controller code :
def create
@image = Image.create(@params['image'])
Camera::choose_picture(url_for :action => :camera_callback)
""
end
def camera_callback
if @params['status'] == 'ok'
@image = Image.find(:first, :conditions =>{'image_uri'=> ''})
@image.update_attributes({'image_uri' => @params['image_uri']}) if @image
end
WebView.navigate( url_for :action => :index )
""
end
But if you're from Spritle, I guess you already have an app (Change Bad to Good) where you allow the user to click a picture and enter the description for it. How did you do it there?
Points: 0
You voted ‘up’
I should add that it works, except that the image is not displayed
I used <img src="<%= @image.image_uri %>" /> in show.erb, and I found in the log, this line :
HttpServer| The file /app/Image/{143481959947002.03}/db/db-files/Image_06-19-2013_10.36.12__006.jpg was not found
On device, the image file is actually in appfolder/rho/db/db-files
I am attaching the log file to avoid clutter.
How should I proceed? Please advise.
Message was edited by: Mark Nongkhlaw
Points: 0
You voted ‘up’
Use "Rho::RhoApplication.get_blob_path(relativeFilePath)" that will return the absolute path to a blob (such as a digital photo).
You can display it as
Thats the logic we used at Spritle app as well.
Visnupriya R
Kutir Mobility.
Points: 0
You voted ‘up’
OK, that webinar app is here : https://github.com/rhomobile/webinar-samples/tree/master/imageupload
Take a look at webinar-samples / imageupload / app / Image / index.erb :
This snippet :
<% @images.each do |obj| %>
<li>
<img src="<%= obj.image_uri %>" width="300px"></img>
</li>
works perfectly fine in this app, but not in mine. Wonder why.
Does layout.erb have anything to do with it?
Points: 0
You voted ‘up’
Does it have something to do with your old Rhodes version and latest Rhodes (3.5.1.13) ?
Points: 0
You voted ‘up’
No, I do not use Rhodes 3.5.1.13. My Rhodes version is 3.3.2 and I tested the webinar app and my app in the same version (3.3.2)
Points: 0
You voted ‘up’
Can you send the zip file of your sample app ?
Visnupriya
Kutir Mobility
Points: 0
You voted ‘up’
You want the one which does NOT work?
Points: 0
You voted ‘up’
yes
Points: 0
You voted ‘up’
Attached.
I used the webinar's app as a template, hoping that it will work. So it contains the old-style (jQTouch).
I identified the problem is in show.erb where log shows:
HttpServer| The file /app/Image/{143481959947002.03}/db/db-files/Image_06-19-2013_10.36.12__006.jpg was not found
That means my code is sending the above URL to the server instead of
/app/db/db-files/Image_06-19-2013_10.36.12__006.jpg
Hope you will be able to trubleshoot now
Thanks a lot!
Points: 0
You voted ‘up’
I see nothing wrong in your code.
Have you tried it on actual device ?
Visnupriya
Kutir Mobility
Points: 0
You voted ‘up’
Its working now.
I changed show.erb to display the image like this :
<li>
<img src="<%=Rho::RhoApplication::get_blob_path(@image.image_uri)%>" width='300px'></img>
</li>
Not sure why it wasn't working earlier. Thanks anyway for your valuable time.
Points: 0
You voted ‘up’