I am using Carrierwave for my images. I added a following section to my app, however in the results it is showing the full size image. How can I modify it so that it uses the thumbnail version of the image :profile?
show.follow.html.erb:
<%= render #users %>
Users controller:
def show
#user = User.find_by(username: params[:id])
#similar_users = #user.similar.shuffle.first(8)
respond_to do |format|
format.html { render layout: 'new_application' }
end
end
def index
#user = current_user
#search = Search.new
#users = #user.present? ? User.where('id != ?',#user.id) : User.all
render layout: 'new_application'
end
def follow
#title = "Following"
#user = User.find_by(username: params[:id])
friend = User.find_by(username: params[:id])
current_user.follow! friend unless current_user.following? friend
#users = #user.followed_users(page: params[:page])
render 'show_follow', layout: 'new_application'
end
To post the profile version of your uploaded image use this in your view code.
<%= image_tag #user.image_url(:profile) %>
You can define the custom thumb size and normal size
as given below
profile :resize_to_fit => [800, 800]
version :thumb do
profile :resize_to_fill => [200,200]
end
You can resize using css to if you want
Related
I am trying to use paginate with act as votable but I'm getting a few errors. My favorites method is to display a page that shows only items that the user upvoted.
def favorites
#title = "Favorites"
#user = User.find(params[:id])
#favorites = #user.find_up_voted_items
render 'show_favorites'
end
This shows all upvoted via act as votable items. However, when I try to use paginate on the page by editing the # favorites with this:
#favorites = #user.find_up_voted_items.paginate(page: params[:page])
I get an undefined method for paginate.
I am already using paginate for other pages:
def index
#users = User.paginate(page: params[:page], per_page: 30)
end
def show
#user = User.find(params[:id])
#microposts = #user.microposts.paginate(page: params[:page])
end
def following
#title = "Following"
#user = User.find(params[:id])
#users = #user.following.paginate(page: params[:page])
render 'show_follow'
end
def followers
#title = "Followers"
#user = User.find(params[:id])
#users = #user.followers.paginate(page: params[:page])
render 'show_follow'
end
What am I missing with my favorites method?
I let users to upload photos using paperclip, but there is no ownership in the photo. Which means, I don't know who uploaded the photos at all.
Here, I would like when someone uploads a picture you know which user uploaded it. i have a user_id column. but i dont know how to implement the code in the pic controller
How do I do that? Thanks!
class PicsController < ApplicationController
before_action :find_pic, only: [:show, :edit, :update, :destroy]
def index
#user = User.find( params[:user_id])
#pics = Pic.all.order("created_at DESC")
end
def show
end
def new
#pic = Pic.new
end
def create
#pic.user = current_user
#pic = Pic.new(pic_params)
if #pic.save
redirect_to #pic, notice: "Yes it was posted"
else
render 'new'
end
end
def edit
#user = User.find(params[:user_id])
#profile = #user.profile
end
def update
if #pic.update(pic_params)
redirect_to #pic, notice: "Congrates Pic was upaded"
else
render 'new'
end
end
def destroy
#pic.destroy
redirect_to users_path
end
private
def pic_params
params.require(:pic).permit(:title, :description, :profile_id)
end
def find_pic
#pic = Pic.find(params[:id])
end
end
class UsersController < ApplicationController
before_action :authenticate_user!
def index
#pics = Pic.all.order("created_at DESC")
end
def show
#user = User.find(params[:id])
#pics = User.find_by(user_name: params[:user_name])
end
end
class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :only_current_user
def new
#user = User.find( params[:user_id])
#profile = Profile.new
end
def create
#user = User.find(params[:user_id])
#profile = #user.build_profile(profile_params)
if #profile.save
redirect_to user_path( params[:user_id])
else
render action: :new
end
end
def edit
#user = User.find(params[:user_id])
#profile = #user.profile
end
def update
#user = User.find(params[:user_id])
#profile = #user.profile
if #profile.update_attributes(profile_params)
flash[:success] = "Profile Updated"
redirect_to user_path(params[:user_id])
else
render action: :edit
end
end
private
def profile_params
params.require(:profile).permit(:avatar, :user_name, :contact_email, :description)
end
def only_current_user
#user = User.find(params[:user_id])
redirect_to(root_url) unless #user == current_user
end
end
If the user can be identified during the upload process, you can try to pass the user_id in a hidden_field during upload. You said you already created the user_id column.
<%= f.hidden_field :user_id , value: #user.id %>
for this code to work you need to find the #user in your controller action. Similar to what you are doing already in your 'index' action: find user by the :user_id
If you are using devise for User you can use current_user.id instead.
Having some trouble trying to get this to work in Rails 4 -
http://railscasts.com/episodes/182-cropping-images?view=comments
As per one of the questions in the comments: using the after_update callback to update the images, it ran into an infinite loop
Apparently the fix is to put #user.avatar.reprocess! directly in the controller instead. However I am not sure where exactly in the controller this should go. And if I'm putting this in the right place is it going to work with rails 4?
I have tried the following with no luck:
def create
#user = User.new(user_params)
if #user.save
if user_params[:avatar].blank?
#user.avatar.reprocess!
flash[:notice] = "Successfully created user."
redirect_to #user
else
render :action => "crop"
end
else
render 'new'
end
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
if user_params[:avatar].blank?
#user.avatar.reprocess!
flash[:notice] = "Successfully updated user."
redirect_to #user
else
render :action => "crop"
end
else
render :action => 'edit'
end
end
my update action:
def update
#user=User.find(params[:id])
#user.update_attributes(user_params)
if #user.cropping?
#user.profile_image.reprocess!
end
if #user.save!
redirect_to user_path(#user)
end
end
and my cropper.rb
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"]
else
super
end
end
def crop_command
target = #attachment.instance
if target.cropping?
["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
end
end
end
end
This has worked for me nicely.
You can read more about this issue via Paperclip - Issue #866.
User jgarber describes a hack to workaround it, using the following:
def reprocess_photo
photo.assign(photo)
photo.save
end
I have three html pages.
In the first page, I view the list of data.
In the second page, I view the data of a particular element of the list.
In the third page, the user can edit data of a particular element of the list.
When the user submits the 'form', how can I redirect the user in the second page? I tried in this way:
render :action => "show_details",:id=>params[:id]
It works. The link is correct. But the page is not opened if I do not refresh the page.
UPDATE I
I write my code in this action in the reports controller:
def setFixed
rs=Report.find(params[:id])
rs.state ="1"
rs.save
render :action => "show_details",:id=>params[:id]
end
UPDATE II
Reports controller code:
class ReportsController < ApplicationController
before_filter :authenticate_user, :only => [:index,:show,:show_details,:new]
def stateDialog
render :stateDialog, :current_state=>params[:current_state]
end
def setFixed
rs=Report.find(params[:id])
rs.state ="1"
rs.save
render :action=>"show_details",:id=>params[:id]
end
def setNotFixed
rs=Report.find(params[:id])
rs.state ="0"
rs.save
render :action=>"show_details",:id=>params[:id]
end
def edit
#report=Report.find(params[:id])
end
def update
#report = Report.find(params[:id])
if #report.update_attributes(params[:report])
flash[:notice]=''
render :action=>"show_details",:id=>params[:id]
else
flash[:notice]=''
render :action=>"show_details",:id=>params[:id]
end
end
def deleteDialog
render "deleteDialog"
end
def focus_maps
render "focus_maps"
end
def delete
Report.find(params[:id]).destroy
render "show"
end
def index
#report=Report.new
end
def logged
render "new"
end
def show
render params[:page]
end
def new
#report=Report.new
end
def show_details
render "show_details"
end
def create
#report=Report.new( params[:report] )
if #report.save
flash[:notice]='Segnalazione avvenuta!'
else
flash[:notice]='Impossibile sottoporre la segnalazione!'
end
render "show"
end
end
I found advice that params must be filled before render calling, like this:
#id = params[:id]
render :action => 'show_details'
This solution is works for me, try it
If you want to redirect user somewhere you should use redirect_to
redirect_to action: 'show_details', id: params[:id]
I'm having an issue where my article_controller.rb's create method is redirecting to the index when the article.save fails due to invalid input by the user. The articles creation url is /articles/new but when the submit fails, I'm redirected to /articles. The form is still available in /articles exactly as it was on /articles/new. The desired behavior would be to return to the /articles/new with whatever the user may have entered repopulated in the form. Is there a way to do this? Here are some of the code snippets to illustrate what's going on.
Here is the article new method:
def new
#article = Article.new
respond_to do |format|
format.html
end
end
Here is the article create method:
def create
#article = current_user.articles.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to(#article, :notice => 'Article was successfully created.') }
else
format.html { render 'new' }
end
end
end
Here is the form:
<%= form_for(#article) do |f| %>
.....
<% end %>
I'm eventually hoping to get this working with a :remote => :true call in the form_for, but just want to get it working first the way it is. Any suggestions?
Try
format.html { render :action => "new" }
And if you are using Rails 3+, try writing your controller something like this DRY.
class ArticlesController < ApplicationController
respond_to :html
def new
#article = Article.new
respond_with #article
end
def create
#article = Article.new(params[:article])
#article.save
respond_with(#article)
end
end