Rails: displaying an image from a blob field in a database - mysql

So, I managed to get an image blob into my MySQL database (there's a big hex number in the field), but I can't find any documentation on how to display the image in a rails environment... when it prints out, it starts with a GIF89... and then the gobbledygook characters you see in a GIF when you open it in Notepad. : P Any clues would be greatly appreciated!
Thanks.

The following code should work. In your controller, create a method:
def show_image
#user = User.find(params[:id])
send_data #user.image, :type => 'image/png',:disposition => 'inline'
end
In your view:
<%= image_tag url_for(:controller => "mycontroller", :action => "show_image", :id => #user.id) %>
I would recommend using the Paperclip gem. It makes saving/viewing of images really easy.

Because you mentioned "quick and dirty", I'll throw out this as an alternative-
<%= ('<img src="data:image/jpg;base64,%s">' % Base64.encode64(#the_data)).html_safe %>
I think this is closest to what you wanted to do. There's a few reasons that this code shouldn't be used as-is, but it's simple. I'd have to think more about how bad of an idea is it is to mark the whole thing as html_safe. Also, this wouldn't work in older versions of IE.

You should also add
resources :users do
get 'show_image', :on => :collection
end
or
get users/show_image" => "users#show_image"
before
resources :users in route.rb file

Related

Editing comments on show.html.erb rather than redirecting to new edit.html.erb page with Ruby on Rails

Like the title says, I am created the blog application from the Rails guide and I want to be able to edit comments made on articles without redirecting to an edit.html.erb page. I want a div that was previously hidden to appear in the middle of the screen with the edit form on it and the text fields filled with the comment that is being edited. Here is an image of the div that is going to appear in the middle of the screen.
I know how to edit comments by going to edit.html.erb but not in the same page or if this is even possible.
I am not looking for help with the css or html, but if there is a way to do this with js or rails that would be awesome. Thanks in advance!
Edit: So the answer by Satendra gets the partial to show up, but the fields do not contain the original comment that I wanted to edit. How can I get the text-fields to be populated with the comment?
Follow these steps to make edit comment on same page.
Create a partial name _edit.html.erb put your edit.html.erb code there.
Create a div in middle of the screen where you want to render this partial.
<div id="edit_comment" >
</div>
put :remote => true to your edit button link.
# change your path accordingly
<%= link_to edit_comment_path(:comment_id => comment.id), :remote => true %>
The :remote => true is the most important part here, it allows the whole Ajax business in the first place.
In comment_controller.rb inside edit function change respond_to js
def edit
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
end
Create edit.js.erb and put below code there.
$("#edit_comment").html("<%= j render(:partial => 'edit', :locals =>{ :comment => #comment }) %>");
# use comment variable in your partial as it is passed as local.
Its Done.

Rails: How to convert <img src= > to image_tag in rails app

This is my first post here and it might sound awfully stupid. Im building my first rails app.
I have this line in my index.html.erb
<img src="/assets/rand_front/<%= #random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;' >
I want to use image_taginstead of the img src
What is the correct way to wrap it around the code?
So far I've tried <%= image_tag ( "/assets/rand_front/<%= #random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;') %>
and
<%= image_tag ( "/assets/rand_front/<%= #random_image%>"), style='height:50vw;width:100vw;margin-bottom:20px;' %>
and many other versions, but none seems to work, what am I doing wrong? and how should I write it properly?
this <%= #random_image%> bit is taking this variable from the index method in the controller.
def index
#products = Product.all.order(created_at: :desc).group_by(&:category_id)
#images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg"]
#random_no = rand(10)
#random_image = #images[#random_no]
end
<%= image_tag "rand_front/#{#random_image}", style: 'height:50vw;width:100vw;margin-bottom:20px;' %>
image_tag will automatically add assets at start of the path
check Image Tag for Documentation
This is what Ruby on Rails is explicitly processing
<%= image_tag("source", {:style => "width:100px;"}) %>
(meaning that Ruby allows you to use this code with out the ( { } ),
For me, first starting out its important to know that this is how Ruby on Rails actually running the code.
In other words YES you could leave the ( { } ) formality out because Ruby will understand your code, hope that helps to clarify some...
If I am not mistaken you have a rand_front folder in your assets folder so you should call image_tag("#{#random_image}") since by default the image_tag helper should check all the folders in the assets directory for the image name
For the CSS properties you can consider using the options hash which would allow you to pass in the CSS properties as keys with your desired values
image_tag("#{#random_image}", height: 20, width: 20) You can check out the documentation in the previous answer

Ruby On Rails: +1 option/vote button error

I'm learning ROR and working on a voting style application, I've got my four tables up and running Users, Questions, Options and Answers and I can write to the Answers table via MySQL and the total votes are displaying so far which is great.
I'm having a problem with getting a +1 button working (eventually it'll be unique per user but for now I just want to see it working).
I'm fairly certain my files are all setup correctly but I'm confused with what to put in my Questions "Show" view for the button, it might be a routes problem as it does say undefined method 'upvote_option_path' - any help would be greatly appreciated! If I've forgotten any files below let me know :-)
Question's Show View:
<% #question.options.each_with_index do |option, index| %>
<p><%= option.option_text %></p>
<p><%= pluralize(option.answers.count, "vote") %><br><br></p>
<p><%= button_to '+1', upvote_option_path(option), method: :post %></p>
<% end %>
Option's Controller upvote:
def upvote
#question = Question.find(params[:question_id])
#option = Option.find(params[:option_id])
Answer.create(user_id: current_user.id, question_id: #question, option_id: #option)
end
Routes file:
resources :questions do
resources :options do
post 'upvote'
end
end
rake routes:
question_option_upvote POST /questions/:question_id/options/:option_id/upvote(.:format) options#upvote
undefined method upvote_option_path
According to your routes, there is no upvote_option_path, it should be question_option_upvote_path. Also the path takes two arguments as keys(:question_id & :option_id), so you need to pass those two to the path.
The below should work
<%= button_to '+1', question_option_upvote_path(#question, option), method: :post %></p>
In rake routes you have this: question_option_upvote, in your view you have this: upvote_option_path. Pretty sure your view should say question_option_upvote_path(option).

Rails - How do you render the html version of a view in one link, and the json.builder version in another link?

I am fairly new to Ruby on Rails but I have been dabbling at a toy app (Practice Management), for practice.
I was able to render a fullcalendar-rails version of my appointments successfully for one link(using appointments_path, and a .json.jbuilder file). However, I now want a list version of the appointments index in html format but I can't seem to find an answer online.
I still want to use the same path (appointments_path), and the same information but with a different format.
Any help is very much appreciated!
Here is my code:
appointments_controller.rb
def index
#appointments = Appointment.all
respond_to do |format|
format.json
format.html
end
end
index.json.jbuilder
json.array! #appointments do |appointment|
json.start appointment.starts_at
json.title appointment.patient.initials
json.url edit_appointment_url(appointment.id)
end
index.html.erb
<div class="appointments-index" id="appointments-index-body">
<% if current_user.appointments.empty? %>
You have no scheduled appointments.
<% else %>
<div id='appointments' class="appointments-body">
<div>
<% end %>
</div>
After implementing appointments_path(format: :json) to either the link for a simple list or the fullcalendar-rails, I got this:
[{"start":"2014-02-13T14:45:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/112/edit"},{"start":"2014-02-
13T16:00:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/113/edit"}, {"start":"2014-02-13T15:00:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/114/edit"},{"start":"2014-02- 13T15:15:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/115/edit"}, {"start":"2014-02-18T10:15:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/116/edit"},{"start":"2014-02- 13T11:00:00.000Z","title":"Beast, A.","url":"http://localhost:3000/appointments/117/edit"}]
providing the id='appointments' in the after else makes sure that fullcalendar-rails will be applied in this div.
Please let me know if there's any info I could add to make this clearer. Thanks!
You can specify format in url helper, like this:
appointments_path(format: :json)
Hope this helps :)

Rails page has wrong html

I have a link to a page in the same folder as this page on my rails site:
<%= link_to 'Special Access', 'followers/special_access' %>
However, when I go to this page it shows a different page on that url.
<p id="notice"><%= notice %></p>
<div id="sent">
<p>Your request has been sent</p>
<%= link_to 'Home', followers_path %>
</div>
I tried deleting the page that the html is from, but first of all I need that page and it also gives me an error.
I edited the controller to contain:
def special_access
format.html { redirect_to followers/special_access }
format.json { render :json => #post }
end
instead of
def show
but that still didn't solve the problem.
How do I get the right html to show up on the right page?
If you do not define a route for special_access, rails will just assume the the special_acces part in the path is the :id of the route for the show page (as the urls look like followers/:id).
So first off, in your routes.rb, find the resources :followers and replace with the following:
resources :followers do
collection do
get :special_access
end
end
And now you should best always use the rails path helpers, so your link would become
<% link_to 'Special Access', special_access_followers_path %>
Here I was assuming the special access is on the collection of the followers, if it should be on a specific follower (which seems more logical to me, but I have no idea of course), you should write
resources :followers do
member do
get :special_access
end
end
And your link would become
<% link_to 'Special Access', special_access_followers_path(#follower) %>
I am not quite sure what you want to do in your controller action, I hope you just want to render an html page (because redirecting to the same url seems silly, and your syntax is wrong there too).
Hope this helps.