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
Ruby - How to Retrieve Sum in Array Group by Multiple Keys with Condition Max
How to Verify Pacts Against an API That Requires an Auth Token
Utc Time Resets to 2000-01-01 (Ruby). How to Prevent the Time from Resetting
Don't the Ruby Methods Instance_Eval() and Send() Negate the Benefits of Private Visibility
Rails 4 Order by Virtual Attribute
Count Overlapping Regex Matches in Perl or Ruby
How to Ignore Irrelevant Methods When Profiling Ruby Applications
Instagram Ruby Gem - Unable to Reach Callback Url
Enter & Ioerror: Byte Oriented Read for Character Buffered Io
Connection Refused Using Sunspot and Solr in Rails
Rails 4 Use Application Helpers Inside Initializers
How to Capture Terminal Arrow Keys in Ruby
Rails 3 + Daemons Gem: Exception When Querying Model
Ruby Instance Method & Conditional Local Variable Assignment with Same Name
Numeric Literals Prepended with '0'
Encrypt Data Bag from Inside of Ruby Without Relying on Knife