ruby on rails - How to use private submit to hide from profile? -
when user submits via private
how can hide submitted info feed , other users being able see on public profile?
<%= button_tag(type: 'submit', class: "btn") %> ... <%= button_tag(type: 'submit', class: "btn", id: "2", name: 'private') %> ...
we put below in controller, since private button in lot of different _forms, have put in each controller or can put in application controller?
if params[:private] # private action / need put here? else # normal submit / , here?
i followed railcast episode t build activity feed: http://railscasts.com/episodes/406-public-activity.
here 's code public profile:
users_controller.rb
def show @user = user.find(params[:id]) @habits = @user.habits @valuations = @user.valuations @accomplished_goals = @user.goals.accomplished @unaccomplished_goals = @user.goals.unaccomplished @averaged_quantifieds = @user.quantifieds.averaged @instance_quantifieds = @user.quantifieds.instance end
show.html.erb
<% if @user.habits.any? %> <h2>habits</h2> <h4>challenges</h4> <%= render partial: 'habits', locals: {habits: @habits} %> <% end %> <% if @user.valuations.any? %> <h2>values</h2> <%= render @valuations %> <% end %> <% if @user.goals.any? %> <h2>goals</h2> <h4> current</h4> <%= render @unaccomplished_goals %> <% end %> <% if @user.goals.any? %> <h4>accomplished</h4> <%= render @accomplished_goals %> <% end %> <% if @user.quantifieds.any? %> <h2>stats</h2> <h4>averaged</h4> <%= render partial: 'averaged', locals: {habits: @averaged_quantifieds} %> <% end %> <% if @user.quantifieds.any? %> <h4>instance</h4> <%= render partial: 'instance', locals: {habits: @instance_quantifieds} %> <% end %>
as requested :)
user model
class user < activerecord::base has_many :authentications has_many :habits, dependent: :destroy has_many :levels has_many :valuations, dependent: :destroy has_many :comments, as: :commentable has_many :goals, dependent: :destroy has_many :quantifieds, dependent: :destroy has_many :results, through: :quantifieds accepts_nested_attributes_for :quantifieds, :reject_if => :all_blank, :allow_destroy => true accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true has_many :active_relationships, class_name: "relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest validates :name, presence: true, length: { maximum: 50 } valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: valid_email_regex }, uniqueness: { case_sensitive: false }, unless: -> { from_omniauth? } has_secure_password validates :password, length: { minimum: 6 } def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap |user| user.provider = auth.provider user.uid = auth.uid user.name = auth.info.name user.oauth_token = auth.credentials.token user.oauth_expires_at = time.at(auth.credentials.expires_at) user.password = (0...8).map { (65 + rand(26)).chr }.join user.email = (0...8).map { (65 + rand(26)).chr }.join+"@mailinator.com" user.save! end end # returns hash digest of given string. def user.digest(string) cost = activemodel::securepassword.min_cost ? bcrypt::engine::min_cost : bcrypt::engine.cost bcrypt::password.create(string, cost: cost) end # returns random token. def user.new_token securerandom.urlsafe_base64 end # remembers user in database use in persistent sessions. def remember self.remember_token = user.new_token update_attribute(:remember_digest, user.digest(remember_token)) end # forgets user. not sure if remove def forget update_attribute(:remember_digest, nil) end # returns true if given token matches digest. def authenticated?(attribute, token) digest = send("#{attribute}_digest") return false if digest.nil? bcrypt::password.new(digest).is_password?(token) end # activates account. def activate update_attribute(:activated, true) update_attribute(:activated_at, time.zone.now) end # sends activation email. def send_activation_email usermailer.account_activation(self).deliver_now end def create_reset_digest self.reset_token = user.new_token update_attribute(:reset_digest, user.digest(reset_token)) update_attribute(:reset_sent_at, time.zone.now) end # sends password reset email. def send_password_reset_email usermailer.password_reset(self).deliver_now end # returns true if password reset has expired. def password_reset_expired? reset_sent_at < 2.hours.ago end def good_results_count results.good_count end # returns status feed. def feed following_ids = "select followed_id relationships follower_id = :user_id" habit.where("user_id in (#{following_ids}) or user_id = :user_id", user_id: id) valuation.where("user_id in (#{following_ids}) or user_id = :user_id", user_id: id) goal.where("user_id in (#{following_ids}) or user_id = :user_id", user_id: id) quantified.where("user_id in (#{following_ids}) or user_id = :user_id", user_id: id) end # follows user. def follow(other_user) active_relationships.create(followed_id: other_user.id) end # unfollows user. def unfollow(other_user) active_relationships.find_by(followed_id: other_user.id).destroy end # returns true if current user following other user. def following?(other_user) following.include?(other_user) end private def from_omniauth? provider && uid end # converts email lower-case. def downcase_email self.email = email.downcase unless from_omniauth? end # creates , assigns activation token , digest. def create_activation_digest self.activation_token = user.new_token self.activation_digest = user.digest(activation_token) end end
user controller
class userscontroller < applicationcontroller before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers] before_action :correct_user, only: [:edit, :update] before_action :admin_user, only: :destroy def index @users = user.paginate(page: params[:page]) end def show @user = user.find(params[:id]) @habits = @user.habits @valuations = @user.valuations @accomplished_goals = @user.goals.accomplished @unaccomplished_goals = @user.goals.unaccomplished @averaged_quantifieds = @user.quantifieds.averaged @instance_quantifieds = @user.quantifieds.instance end def new @user = user.new end def create @user = user.new(user_params) if @user.save @user.send_activation_email flash[:info] = "please check email activate account." redirect_to root_url else @feed_items = [] render 'pages/home' end end def edit @user = user.find(params[:id]) end def update @user = user.find(params[:id]) if @user.update_attributes(user_params) flash[:success] = "profile updated" redirect_to @user else render 'edit' end end def destroy user.find(params[:id]).destroy flash[:success] = "user deleted" redirect_to users_url 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 private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end # before filters # confirms logged-in user. def logged_in_user unless logged_in? store_location flash[:danger] = "please log in." redirect_to login_url end end # confirms correct user. def correct_user @user = user.find(params[:id]) redirect_to(root_url) unless current_user?(@user) end # confirms admin user. def admin_user redirect_to(root_url) unless current_user.admin? end end
update
with k's answer below error message upon going users or users/1, users/2 etc..
started "/users/1" 127.0.0.1 @ 2015-04-01 16:32:13 -0400 syntaxerror (/users/galli01anthony/desktop/pecoce/app/controllers/users_controller.rb:79: syntax error, unexpected ':', expecting keyword_end users_attributes: [:name, :email, :password, :... ^ /users/galli01anthony/desktop/pecoce/app/controllers/users_controller.rb:79: syntax error, unexpected ',', expecting keyword_end ...ivate, :password_confirmation], valuations_attributes: [:nam... ... ^ /users/galli01anthony/desktop/pecoce/app/controllers/users_controller.rb:79: syntax error, unexpected '=', expecting keyword_end ... [:name, :tag_list, :private] = true ... ^ /users/galli01anthony/desktop/pecoce/app/controllers/users_controller.rb:81: syntax error, unexpected ':', expecting keyword_end users_attributes: [:name, :email, :password, :... ^ /users/galli01anthony/desktop/pecoce/app/controllers/users_controller.rb:81: syntax error, unexpected ',', expecting keyword_end ...sword, :password_confirmation], valuations_attributes: [:nam... ... ^): app/controllers/users_controller.rb:79: syntax error, unexpected ':', expecting keyword_end app/controllers/users_controller.rb:79: syntax error, unexpected ',', expecting keyword_end app/controllers/users_controller.rb:79: syntax error, unexpected '=', expecting keyword_end app/controllers/users_controller.rb:81: syntax error, unexpected ':', expecting keyword_end app/controllers/users_controller.rb:81: syntax error, unexpected ',', expecting keyword_end
this two-part question. find second part here: how use private submit hide feed?
add field 'private' user model default value 'false'. normal user informations flagged 'public' (because private field has value false) if params[:private], value of private field set 'true'.
next can add method user model grab data of user private = false flag (for public views).
edit:
displaying public or private:
add field 'private' each of related models possibly marked private. don't forget add in migrations. set private's default false.
include in valuation & user migration/schema
t.boolean :private, default: false
valuation.rb
def public? private == true ? false : true end
user.rb
# gets public valutations or nil, if there's no public valutation def public_valuations valuations.find(&:public?) end
do in same way each of wanted relations. enables public informations via
@valuations = @user.public_valuations
your current show action displays additional user's informations - public , private - should displayed if current_user = @user.
at last have insert condition in show action:
def show @user = user.find(params[:id]) if current_user == @user @habits = @user.habits @valuations = @user.valuations @accomplished_goals = @user.goals.accomplished @unaccomplished_goals = @user.goals.unaccomplished @averaged_quantifieds = @user.quantifieds.averaged @instance_quantifieds = @user.quantifieds.instance else @valuations = @user.public_valuations end end
that solution depends on current_user, i.e. must have method returns object of logged_in user (maybe in session). michael hartl wrote awesome tutorial user authentication. *rubyonrailsbeginner used hartl tutorial :)
creating public or private records
since had set private's default false, can use existing code create public entries.
for private entries must set corresponding attribute in user_params true.
edit params.require:
i set [:private] in else clause explicit false, user might set private attributes public, if wanted.
def user_params if params[:private] = true params.require(:user).permit(:name, :email, :password, :private, :password_confirmation, valuations_attributes: [:name, :tag_list, :private]) else params[:user][:valuations][:private] = false params.require(:user).permit(:name, :email, :password, :password_confirmation, valuations_attributes: [:name, :tag_list]) end end
the rails api gives hints strong parameters nested attributes.
hope helps!
Comments
Post a Comment