How to Use Callback to Set :Level Value? - ruby-on-rails

I'm creating a habit tracking app.
When a User creates a habit he will put in its :date_started and which days he is :committed to doing the habit.
Then after X amount of :committed days the habit moves onto the following :levels until eventually it hits "mastery" (days in the levels are broken down in the model below).
I already figured out date_started.
Now how can we use committed days, instead of just any day, to calculate what level a habit is on?
:levels is a dynamic getter method, which currently has no effect on the database.
I was told I need an appropriate callback to set the :levels attribute before the record is saved, but I don't know how.
Will you accept this challenge?
form
<%= f.label "Committed to:" %>
<%= f.collection_check_boxes :committed, Date::DAYNAMES, :downcase, :to_s %>
class Habit < ActiveRecord::Base
belongs_to :user
validates :action, presence: true
serialize :committed, Array
scope :missed, -> { where(missed: 1) }
scope :nonmissed, -> { where(missed: 0) }
def levels
committed_wdays = committed.map { |day| Date::DAYNAMES.index(day) }
n_days = (date_started..Date.today).count { |date| committed_wdays.include? date.wday }
case n_days
when 0..9
1
when 10..24
2
when 25..44
3
when 45..69
4
when 70..99
5
else
"Mastery"
end
end
end
controller
class HabitsController < ApplicationController
before_action :set_habit, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#habits = Habit.all.order("date_started DESC")
#missed_habits = current_user.habits.missed
#nonmissed_habits = current_user.habits.nonmissed
end
def show
end
def new
#habit = current_user.habits.build
end
def edit
end
def create
#habit = current_user.habits.build(habit_params)
if #habit.save
redirect_to #habit, notice: 'Habit was successfully created.'
else
render action: 'new'
end
end
def update
if #habit.update(habit_params)
redirect_to #habit, notice: 'Habit was successfully updated.'
else
render action: 'edit'
end
end
def destroy
#habit.destroy
redirect_to habits_url
end
private
def set_habit
#habit = Habit.find(params[:id])
end
def correct_user
#habit = current_user.habits.find_by(id: params[:id])
redirect_to habits_path, notice: "Not authorized to edit this habit" if #habit.nil?
end
def habit_params
params.require(:habit).permit(:missed, :left, :level, :date_started, :trigger, :action, :target, :positive, :negative, :committed => [])
end
end
Github: https://github.com/RallyWithGalli/ruletoday
Thanks in advance for your expertise =]

Related

wrong number of arguments (given 1, expected 0)

I want to upload photos to my website. After i selected the photos and click on "Add Photos" this error comes up. Any ideas how i can solve this?
photos_controller.rb
class PhotosController < ApplicationController
def create
#wall = Wall.find(params[:wall_id])
if params [:images]
params[:images].each do |img|
#wall.photos.create(image: img)
end
#photos = #wall.photos
redirect_back(fallback_location: request.referer, notice: "Saved...")
end
end
end
walls.controller.rb
class WallsController < ApplicationController
before_action :set_wall, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
before_action :is_authorised, only: [:listing, :pricing, :description, :photo_upload, :location, :update]
def index
#walls = current_user.walls
end
def new
#wall = current_user.walls.build
end
def create
#wall = current_user.walls.build(wall_params)
if #wall.save
redirect_to listing_wall_path(#wall), notice: "Saved..."
else
fash[:alert] = "Something went wrong..."
render :new
end
end
def photo_upload
#photos = #wall.photos
end
def location
end
def update
if #wall.update(wall_params)
flash[:notice] = "Saved..."
else
flash[:notice] = "Something went wrong..."
end
redirect_back(fallback_location: request.referer)
end
private
def set_wall
#wall = Wall.find(params[:id])
end
def is_authorised
redirect_to root_path, alert: "You don't have permission" unless current_user.id == #wall.user_id
end
def wall_params
params.require(:wall).permit(:size_sqm, :visibility, :traffic, :wall_name, :summary, :address, :price)
end
end
In a Rails controller action, params is a Hash value and a Hash value is fetched like below:
params[:name]
In your code you're having an unwanted space between params and [:images] which means translates to calling a method by name params

Ruby on Rails || Adding conferences to users

I'm creating an app where u can see all conferences in the world.
What I've got:
creating an conference
showing all conferences
Now what I want to create is an button that let's me add a conference to a user.
What do I want to accomplish with this:
adding conference to users
showing added conferences in a list
viewing conferences and adding content
I was thinking of an button that copies the attributes of selected object and adds it to an selected user for future manipulation and viewing of the conference
I'm asking if someone can tell me how to accomplish this
https://consulegem-salman15.c9users.io/conferences
Migration conferences
class CreateConferences < ActiveRecord::Migration[5.0]
def change
create_table :conferences do |t|
t.string :conference
t.string :country
t.string :month
t.string :presence
t.string :audience
t.integer :cost
t.text :content
t.references :user, foreign_key: true
t.timestamps
end
add_index :conferences, [:user_id, :created_at]
end
end
Controller conference
class ConferencesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :admin_user, only: :destroy
def index
#conferences = Conference.paginate(page: params[:page])
if params[:search]
#conferences = Conference.search(params[:search]).order("created_at DESC").paginate(page: params[:page])
else
#conferences = Conference.all.order('created_at DESC').paginate(page: params[:page])
end
end
def new
#user = User.new
#conference = Conference.new
end
def create
#conference = current_user.conferences.build(conference_params)
if #conference.save
flash[:success] = "conference created!"
redirect_to conferences_path
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'new'
end
end
def destroy
#conference.destroy
flash[:success] = "conference deleted"
redirect_to request.referrer || root_url
end
private
def conference_params
params.require(:conference).permit(:conference,:country , :month, :presence, :audience, :cost ,:picture)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def correct_user
#conference = current_user.conferences.find_by(id: params[:id])
redirect_to root_url if #conference.nil?
end
end
Model controller
class Conference < ApplicationRecord
belongs_to:user
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :conference, presence: true, length: { maximum: 140 }
validate :picture_size
scope :conference, -> (conference) { where conference: conference }
def self.search(search)
where("conference LIKE ? OR country LIKE ? OR month LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
end
private
# Validates the size of an uploaded picture.
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
The answer I found was by adding a SubscriptionsController that is nested in the ConferenceController and a RelationshipController
SubscriptionController
class SubscriptionsController < ApplicationController
before_action :set_conference, only: :create
def index
#subscriptions = current_user.subscriptions
#subscriptions = Subscription.paginate(page: params[:page])
end
def show
#subscription = Subscription.find_by(id: params[:id])
end
def create
if current_user.subscriptions.create(conference: #conference)
flash[:success] = "You are now subscribed to { #conference.conference }"
else
flash[:error] = "Could not create subscription."
end
redirect_to conferences_path
end
def destroy
#subscription = current_user.subscriptions.find(params[:id])
if #subscription.destroy
flash[:success] = "You are no longer subscribed to { #conference.conference }"
else
flash[:error] = "Oh noes"
end
redirect_to conferences_path
end
def set_conference
#conference = Conference.find_by id: params["conference_id"]
end
end
RelationshipController
class RelationshipsController < ApplicationController
before_action :logged_in_user
def create
#conference = Conference.find(params[:followed_id])
current_user.follow(#conference)
respond_to do |format|
format.html { redirect_to #conference }
format.js
end
end
def destroy
#user = Relationship.find(params[:id]).followed
current_user.unfollow(#user)
respond_to do |format|
format.html { redirect_to #user }
format.js
end
end
end
ConferenceController
class ConferencesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :admin_user, only: :destroy
def index
#conferences = Conference.paginate(page: params[:page])
if params[:search]
#conferences = Conference.search(params[:search]).order("created_at DESC").paginate(page: params[:page])
else
#conferences = Conference.all.order('created_at DESC').paginate(page: params[:page])
end
end
def show
#conference = Conference.find(params[:id])
end
def new
#user = User.new
#conference = Conference.new
end
def create
#conference = Conference.new(conference_params)
#conference.user = current_user
if #conference.save
flash[:success] = "conference created!"
redirect_to conferences_path
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'new'
end
end
def destroy
#conference.destroy
flash[:success] = "conference deleted"
redirect_to request.referrer || root_url
end
private
def conference_params
params.require(:conference).permit(:conference,:country , :month, :presence, :audience, :cost ,:picture)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def correct_user
#conference = current_user.conferences.find_by(id: params[:id])
redirect_to root_url if #conference.nil?
end
end

undefined local variable or method `new_comment`

NameError in Comments#create
Showing /comments/_form.html.erb
where line #1 raised:
undefined local variable or method new_comment for #<#:0x007fb9760eb640>
<%= form_for new_comment, url: send(create_url, new_comment.commentable) do |f| %>
new_comment is being called by:
activities/index.html.erb
<%= render "comments/form", new_comment: Comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name), create_url: :activity_comments_path %>
comment.rb
class Comment < ActiveRecord::Base
after_save :create_notification #ERROR OCCURRED WHEN I INTRODUCED THIS LINE AND
validates :activity, presence: true #THIS LINE
validates :user, presence: true
has_many :notifications
belongs_to :commentable, polymorphic: true
belongs_to :user
belongs_to :activity
private
def create_notification
Notification.create(
activity_id: self.activity_id,
user_id: self.user_id,
comment_id: self.id,
read: false
)
end
end
notification.rb
class Notification < ActiveRecord::Base
belongs_to :activity
belongs_to :comment
belongs_to :user
end
activity.rb
class Activity < ActiveRecord::Base
self.per_page = 20
has_many :notifications
belongs_to :user
has_many :comments, as: :commentable
belongs_to :trackable, polymorphic: true
def conceal
trackable.conceal
end
def page_number
(index / per_page.to_f).ceil
end
private
def index
Activity.order(created_at: :desc).index self
end
end
routes.rb
resources :activities do
resources :comments
resources :notifications
member do
post :like
post :notifications
end
end
Any ideas on what maybe the cause? This error is coming off of the awesome answer here: How to make a path to a paginated url?
Thank you!
UPDATE
class CommentsController < ApplicationController
before_action :load_commentable
before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(comment_params)
if #comment.save
redirect_to #commentable, notice: "comment created."
else
render :new
end
end
def edit
#comment = current_user.comments.find(params[:id])
end
def update
#comment = current_user.comments.find(params[:id])
if #comment.update_attributes(comment_params)
redirect_to #commentable, notice: "Comment was updated."
else
render :edit
end
end
def destroy
#comment = current_user.comments.find(params[:id])
#comment.destroy
redirect_to #commentable, notice: "comment destroyed."
end
def like
#comment = Comment.find(params[:id])
#comment_like = current_user.comment_likes.build(comment: #comment)
if #comment_like.save
#comment.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
private
def set_comment
#comment = Comment.find(params[:id])
end
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
def comment_params
params[:comment][:user_id] = current_user.id
params.require(:comment).permit(:content, :commentable, :user_id, :like)
end
end
notifications_controller
class NotificationsController < ApplicationController
def index
#notifications = current_user.notifications
#notifications.each do |notification|
notification.update_attribute(:read, true)
end
end
def destroy
#notification = Notification.find(params[:id])
#notification.destroy
redirect_to :back
end
end
activities_controller
class ActivitiesController < ApplicationController
def index
#activities = Activity.order("created_at desc").paginate(:page => params[:page])
end
def show
redirect_to(:back)
end
def like
#activity = Activity.find(params[:id])
#activity_like = current_user.activity_likes.build(activity: #activity)
if #activity_like.save
#activity.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
end
It should be <%= form_for comment do |f| %> not new comment since comment is the element in your database

Limit user to 1 like?

How can we limit a user to 1 like per comment?
comments_controller.rb
def like
#comment = Comment.find(params[:id])
#comment.increment!(:likes)
#comment.create_activity :like
flash[:success] = 'Thanks for liking!'
redirect_to(:back)
end
_comments.html.erb
<% #comments.each do |comment| %>
<%= User.find(comment.user_id).name %>
<%= simple_format comment.content %>
<%= pluralize(comment.likes, 'like') %>
<%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') +
' Like it', like_comment_path(:id => comment.id), method: :post %>
<% end %>
valuation.rb
class Valuation < ActiveRecord::Base
belongs_to :user
has_many :comments, as: :commentable
include PublicActivity::Model
tracked owner: ->(controller, model) { controller && controller.current_user }
end
routes.rb
resources :valuations do
resources :comments
member do
post :like
end
end
I followed along this tutorial: http://www.sitepoint.com/activity-feeds-rails/ to implement the public_activity gem and the like button.
Thank you for your time and expertise!
UPDATE
class CommentsController < ApplicationController
before_action :load_commentable
before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(comment_params)
if #comment.save
#comment.create_activity :create, owner: current_user
redirect_to #commentable, notice: "comment created."
else
render :new
end
end
def edit
#comment = current_user.comments.find(params[:id])
end
def update
#comment = current_user.comments.find(params[:id])
if #comment.update_attributes(comment_params)
redirect_to #commentable, notice: "Comment was updated."
else
render :edit
end
end
def destroy
#comment = current_user.comments.find(params[:id])
#comment.destroy
#comment.create_activity :destroy, owner: current_user
redirect_to #commentable, notice: "comment destroyed."
end
def like
#comment = Comment.find(params[:id])
#comment.increment!(:likes)
#comment.create_activity :like
flash[:success] = 'Thanks for liking!'
redirect_to(:back)
end
private
def set_comment
#comment = Comment.find(params[:id])
end
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
def comment_params
params[:comment][:user_id] = current_user.id
params.require(:comment).permit(:content, :commentable, :user_id)
end
end
comment.rb
class Comment < ActiveRecord::Base
include PublicActivity::Common
# tracked except: :update, owner: ->(controller, model) { controller && controller.current_user }
belongs_to :commentable, polymorphic: true
belongs_to :user
end
I think you could create additional model:
class CommentLike
belongs_to :comment
belongs_to :user
validates :user, uniqueness: { scope: :comment}
end
class User
has_many :comment_likes
end
class Comment
has_many :comment_likes
end
def like
#comment = Comment.find(params[:id])
if current_user.comment_likes.create(comment: #comment)
#comment.increment!(:likes)
#comment.create_activity :like
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
It'll solves you problem. If storing likes_count in comment is neccessary, you could use Rails' counter_cache.

How to Use :date_started to Figure Out :levels?

When a User starts a habit he chooses his date_started in the form.
How can we use date_started as the start point to figure out and display in the index what level the User is on?
A certain amount of days must pass before the User moves onto the next level (as shown below in the model).
Currently when a User looks at the index all his habits will show "Mastered" regardless of what the date_started is. Thanks in advance for your expertise =]
form
<label> Started: </label>
<%= f.date_select :date_started, :order => [:month, :day, :year], class: 'date-select' %>
class Habit < ActiveRecord::Base
belongs_to :user
validates :action, presence: true
serialize :committed, Array
scope :missed, -> { where(missed: 1) }
scope :nonmissed, -> { where(missed: 0) }
def levels
case committed
when 0..9
1
when 10..24
2
when 25..44
3
when 45..69
4
when 70..99
5
else
"Mastered"
end
end
end
controller
class HabitsController < ApplicationController
before_action :set_habit, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#habits = Habit.all.order("date_started DESC")
#missed_habits = current_user.habits.missed
#nonmissed_habits = current_user.habits.nonmissed
end
def show
end
def new
#habit = current_user.habits.build
end
def edit
end
def create
#habit = current_user.habits.build(habit_params)
if #habit.save
redirect_to #habit, notice: 'Habit was successfully created.'
else
render action: 'new'
end
end
def update
if #habit.update(habit_params)
redirect_to #habit, notice: 'Habit was successfully updated.'
else
render action: 'edit'
end
end
def destroy
#habit.destroy
redirect_to habits_url
end
private
def set_habit
#habit = Habit.find(params[:id])
end
def correct_user
#habit = current_user.habits.find_by(id: params[:id])
redirect_to habits_path, notice: "Not authorized to edit this habit" if #habit.nil?
end
def habit_params
params.require(:habit).permit(:missed, :left, :levels, :date_started, :trigger, :action, :target, :positive, :negative, :committed => [])
end
end
Try something like this:
def levels
n_days = Integer(Date.today - date_started)
case n_days
when 0..9
1
when 10..24
2
...
end

Resources