Access Image from Different View in a View with Paperclip Gem Ruby on Rails

Access image from different view in a view with paperclip gem ruby on rails

Firstly, in the causes controller, pluralize @profile because Profile.all will return an array of all profiles. i.e. change @profile = Profile.all to @profiles = Profile.all

Because @profiles is an array, you need to iterate through each array item in the view Causes:

<% @profiles.each do |profile| %>
<%= image_tag profile.images.first.image.url(:thumb) %>
<% end %>

If you only intend on returning a single profile image then you will need to specify which profile in the controller. i.e.

@profile = Profile.first

or if the cause model belongs to the profile model:

@profile = Profile.find(params[:profile_id])

Securely Display an Image Uploaded with paperclip gem

You could use send_file. Assuming you have secure_image action in your routes:

resources :posts do
member do
get :secure_image
end
end

and following method in corresponding controller:

def secure_image
send_file Post.find(params[:id]).some_image.path
end

you can use protected images in your views:

<%= image_tag secure_image_post_path(@post) %>

What's happening here: usually Rails (or nginx or apache) sends static files directly bypassing security checks in sake of speed. But request for secure_image action goes through whole Rails' stack, so it is protected with your authentication system.

Paperclip - get image path without ActiveRecord model

I created a gist for that here

Basically your object needs to implement some ActiveModel modules to fullfil Paperclip requirements, because this library is strongly coupled to ActiveRecord.

Paperclip gem render avatar upload in different view

On your users profile page, you can place a form with update action for your current user. It could look something like so:

users/registrations/show.html.erb

<% if current_user == @user %>
<%= form_for current_user, url: { :action => :update } do |f| %>
<!-- ... -->
<div class="field">
<%= f.label :avatar %><br />
<%= f.file_field :avatar %>
</div>
<!-- ... -->
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% end %>


Related Topics



Leave a reply



Submit