ruby on rails - How to use private submit with activities feed? -


how can give user option make activities private? give users privacy posts want eyes only.

i told code isn't working because might have "not setting 'private' checkbox work correctly", yet private checkbox works hiding submissions on public profiles (just not on activities feed).

class activitiescontroller < applicationcontroller   def index #added .public     @activities = activity.visible.order("created_at desc").where(user_id: current_user.following_ids)   end end   class activity < activerecord::base   belongs_to :user   has_many :comments, as: :commentable   belongs_to :trackable, polymorphic: true   scope :visible, ->{ where(:hidden => false) }    def visible?     !hidden   end end  create_table "activities", force: true |t|   t.boolean "hidden", default: false   t.integer  "user_id"   t.string   "action"   t.integer  "trackable_id"   t.string   "trackable_type"   t.datetime "created_at",     null: false   t.datetime "updated_at",     null: false end 

and in 1 of _forms, such @valuations or @goals, user can make distinction via submission:

 <%= button_tag(type: 'submit', class: "btn", id: "gold")  %>    <span class="glyphicon glyphicon-plus"></span> public  <% end %>   <%= button_tag(type: 'submit', class: "btn") %>    <% :hidden %><span class="glyphicon glyphicon-plus"></span> private  <% end %> 

thank you!

i use enum column instead. enums give tons of functionality such scopes, interrogation , bang methods change status. of enums built extend - lets want add functionality users can have posts viewable friends - adding additional state enum easy!

first add database column. run:

rails g migration addvisiblitytoactivities visibility:integer:index 

then edit migration add default:

class addvisibilitytoactivities < activerecord::migration   def change     t.integer :visibility, index: true, default: 0   end end 

run migration rake db:migrate. need add enum mappings activity model:

class activity < activerecord::base   belongs_to :user   has_many :comments, as: :commentable   belongs_to :trackable, polymorphic: true    # change order if want default private!   enum visibility: [:visible, :hidden]    default_scope { visible.order('created_at desc') } end 

note add default scope. can simplify query in our controller:

class activitiescontroller < applicationcontroller   def index #added .public     @activities = activity.where(user: current_user.following)     # note don't have use ids when creating     # clause association. rails work   end end 

the easiest way let users alter visibility when creating/updating records use select:

<div class="field">   <%= f.label :visibility %>   <%= f.select :visibility, activity.visibilities.keys.map(&:titleize) %> </div> 

just remember whitelist visibility property!

# app/controllers/activities_controller.rb  # ...  def create   @activity = activity.new(activity_params) |a|     a.user = current_user   end    # ... end  # ...  def activity_params   params.require(:activity).permit(:visibility) end 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -