I have a model called Thing and a controller called Things.
I followed this tutorial to try and set a maximum amount of Things a user can create.
Here's the warning: the terminal is giving a warning (not a huge issue) of DEPRECATION WARNING: Passing an argument to force an association to reload is now deprecated and will be removed in Rails 5.1. Please call "reload" on the result collection proxy instead. What should I do to make it go away?
Here's the problem: The line self.errors.add(:base, "Exceeded Things Limit") isn't displaying an alert or notice in the view. How would I achieve this? It's not creating a new Thing (because I met the maximum limit of 2) which is good, but it's just reloading a new form which would be horrible for user experience.
I'm working Rails 5 and Devise.
Here's my Thing model:
class Thing < ApplicationRecord
belongs_to :user
validate :thing_count_within_limit, :on => :create
attr_accessor :validation_code, :flash_notice
def self.search(search)
if search
where("zipcode LIKE ?", "%#{search}%")
else
all
end
end
def thing_count_within_limit
if self.user.things(:reload).count >= 2
self.errors.add(:base, "Exceeded Things Limit")
end
end
end
And here's my Things controller:
class thingsController < ApplicationController
before_action :find_thing, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user_first, only: [:edit, :update, :destroy]
before_action :authorized_pilot, only: [:edit, :update, :destroy, :profile]
def index
#things = Thing.all.order("created_at ASC")
#things = Thing.search(params[:search])
end
def new
#thing = current_user.things.build
end
def create
#thing = current_user.things.build(thing_params)
if #thing.save
redirect_to #thing
else
render "new"
end
end
def profile
#things = Thing.where(user_id: current_user)
end
def show
end
def edit
end
def update
if #thing.update(thing_params)
redirect_to #thing
else
render "edit"
end
end
def destroy
if #thing.destroy
redirect_to root_path
else
redirect_to #thing
end
end
private
def thing_params
params.require(:thing).permit(:title, :description, :image).merge(zipcode: current_user.zipcode)
end
def find_thing
#thing = thing.find(params[:id])
end
def authenticate_user_first
if current_user != thing.find(params[:id]).user
redirect_to #thing
else
end
end
end
Can anyone help? Help is greatly appreciated.
There are two things that aren't connected to each other.
First, there is the deprecation warning. Because it is just a warning, not an error, you can choose to ignore it at the moment. If you want to remove the warning, just follow its instruction and change this line
if self.user.things(:reload).count >= 2
to
self.user.things.reload.count >= 2
Seconds, your code works like expected. Rails validations do not raise any errors, but they add error messages to the object. Just make sure that you display the errors to the user. To display the error you added to :base, add something like the following to your new.html.erb view:
<% if #thing.errors[:base].any? %>
<div class="error_message">
<%= #thing.errors.full_messages_for(:base).to_sentence %>
</div>
<% end %>
Related
I'm trying to add a validation to my Rails app in order to display an error message if the user goes to the wrong id. The project has reviews, if I go to http://localhost:3000/reviews/:id that doesn't exist the app crashes, I'd like to prevent the runtime error by displaying a message.
In the model, I got this validation:
class Review < ApplicationRecord
validates :id, presence: true
end
Then, in the reviews/show.html.erb file, I'm trying this:
<% if #review.valid? %>
<div class='review-header'>
....
</div>
<% else %>
<% #review.errors.objects.first.full_message %>
<% end %>
This is also the Reviews Controller:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :authorize!, only: [:edit, :destroy]
def index
if params[:search]
#reviews = Review.where("title like ?", "%#{params[:search]}%")
else
#reviews = Review.all
end
end
def new
#review = Review.new
#comment = Comment.new
#comment.review_id = #review.id
#We need to declare the comments in the new action.
end
def create
#review = current_user.reviews.new(review_params)
if #review.save
redirect_to review_path(#review)
else
render 'new'
end
end
def show
#comment = Comment.new
#We also need to declare the new comment in the show action.
end
def edit
end
def update
if #review.update(review_params)
redirect_to review_path(#review)
else
render 'edit'
end
end
def destroy
#review.destroy
redirect_to reviews_path
end
private
def set_review
#review = Review.find_by(id: params[:id])
end
def review_params
params.require(:review).permit(:title, :content, :category_id, :search)
end
def authorize!
authorize #review #authorize method using the Pundit gem
end
end
However, my project keep crashing rather than showing a message. If there's any way I can make this work? Thanks.
The whole setup of the question is actually broken.
You don't need to add a model validation for the id since ids are automatically generated by the database when you insert records. On most databases primary keys are also non-nullable. Adding the validation will actually break the model as will prevent you from saving records without manually assigning an id (bad idea).
Its also not the models job to verify that a record can be found in the controller. Instead your controller should use find so that it bails early if the record cannot be found:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :authorize!, only: [:edit, :destroy]
private
def set_review
#review = Review.find(params[:id])
end
end
This halts execution of the method and other callbacks and prevents the NoMethodErrors that are bound to occur. There is no sense in continuing to process a request if the record that its supposed to CRUD doesn't exist.
By default Rails will handle an uncaught ActiveRecord::RecordNotFound exception by rendering a static HTML page located at public/404.html and returning a 404 status code. If you want to customize this on the controller level use rescue_from:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :authorize!, only: [:edit, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :not_found
private
def set_review
#review = Review.find(params[:id])
end
def not_found
# renders app/reviews/not_found.html.erb
render :not_found,
status: :not_found
end
end
Note that this should be done in different view. If you add a <% if #review.present? %> to your reviews/show.html.erb view you should get your Rails licence revoked as the views one and only job is to display the review.
You can also configure the responses on the application level with config.exceptions_app.
The problem is that if the ID does not correspond to a review in the database, the #review object will be nil, and your line if #review.valid? will throw an error.
You need a different test, something like
<% if #review.present? %>
<div class='review-header'>
....
</div>
<% else %>
Review does not exist.
<% end %>
I am a rookie in Ruby on Rails, at the moment, I am developing a Rails application comes with soft delete feature. As you can see in the controller, in the index action, as an admin I will display records that are not included soft-deleted records as default.
But I want to make a checkbox or button on the page to include the soft-deleted records when I checked or click them. And I'm quite stuck right here, if anyone have a solution it would be great. Btw, don't hesitate to suggest a better approach for this situation. Any helps would be appreciated, I attach the controller and model below.
Controller
class BookingsController < ApplicationController
before_action :admin_user, only: %i(edit update)
before_action :load_tour_detail, only: %i(create reduce_quantity cal_revenue)
before_action :load_booking, except: %i(index new create index_with_deleted)
after_action :reduce_quantity, :cal_revenue, only: :create
after_action :increse_quantity, only: :destroy
respond_to :html, :json
def index
#bookings = if current_user.admin?
Booking.includes(:tour_detail).not_deleted
.paginate(page: params[:page])
else
Booking.includes(:tour_detail).where(user_id: current_user.id)
.paginate(page: params[:page])
end
end
Model
class Booking < ApplicationRecord
belongs_to :tour_detail
belongs_to :user
validates :people_number, presence: true
before_save :cal_price
enum status: {pending: 1, confirmed: 2, cancelled: 3}
scope :not_deleted, -> {where("deleted_at IS NULL")}
scope :deleted, -> {where("deleted_at IS NOT NULL")}
def soft_delete
update deleted_at: Time.now
end
def recover
update deleted_at: nil
end
end
UPDATE:
I found a solution for this, I made a button on index page which pass a param to controller. Then on the controller I check the praram is it valid? And redirect my app to the right action that includes soft_deleted records or not. I will post the solution below just in case my explanation in English is too bad.
If someone has any better solution for this, please just suggest, it would be useful for me. Thank you guys!
Controller
class BookingsController < ApplicationController
before_action :admin_user, only: %i(edit update)
before_action :load_tour_detail, only: %i(create reduce_quantity cal_revenue)
before_action :load_booking, except: %i(index new create index_with_deleted)
after_action :reduce_quantity, :cal_revenue, only: :create
after_action :increse_quantity, only: :destroy
respond_to :html, :json
def index
#bookings = if current_user.admin?
if params.has_key?(:soft_deleted)
Booking.includes(:tour_detail).all
.paginate(page: params[:page])
else
Booking.includes(:tour_detail).not_1deleted
.paginate(page: params[:page])
end
else
Booking.includes(:tour_detail).where(user_id: current_user.id)
.paginate(page: params[:page])
end
end
View
<% if params.has_key?(:soft_deleted) %>
<%= link_to bookings_path, class: "btn btn-sm btn-primary" do %>
Only existed bookings
<% end %>
<% else %>
<%= link_to bookings_path(soft_deleted: true), class: "btn btn-sm btn-warning" do %>
Included deleted bookings
<% end %>
<% end %>
You can use something like below:
def index
#bookings = if current_user.admin?
Booking.includes(:tour_detail)
.deleted(params[:checkbox_input_name] # ----------> This line here
.paginate(page: params[:page])
else
Booking.includes(:tour_detail)
.where(user_id: current_user.id)
.deleted(params[:checkbox_input_name] # ----------> This line here
.paginate(page: params[:page])
end
end
And then in your model, add a scope like below:
class Booking < ApplicationRecord
# -----------------
scope :deleted, -> lambda { |deleted_at| where("deleted_at IS NULL") if deleted_at.present? }
# The above scope will return all objects if deleted_at param is not present.
private
def soft_delete
update deleted_at: Time.now
end
def recover
update deleted_at: nil
end
end
I've made a very simple blog where users can Create, Edit and Delete posts however I want to add functionality where users can only Edit for a limited time (say 3 days). My understanding of Ruby is not strong enough to know how to do this so any help is appreciated.
This is my Notes (my name for Posts) controller
class NotesController < ApplicationController
before_action :find_note, only: [:show, :edit, :update, :destroy]
def index
#notes = Note.where(user_id: current_user)
end
def show
end
def new
#note = current_user.notes.build
end
def create
#note = current_user.notes.build(note_params)
if #note.save
redirect_to #note
else
render 'new'
end
end
def edit
end
def update
if #note.update(note_params)
redirect_to #note
else
render 'edit'
end
end
def destroy
#note.destroy
redirect_to notes_path
end
private
def find_note
#note = Note.find(params[:id])
end
def note_params
params.require(:note).permit(:title, :content)
end
end
I assume somewhere in the edit method I need to write a rule for restricting the ability to edit posts to only 3 days, using the created_at function somehow? I'm just at a loss as to exactly how to do this.
Any help is appreciated.
Perfect solution for that is :before_filter
class NotesController < ApplicationController
before_filter :check_time!, only: [:edit, :update]
def edit
end
def create
end
private
def check_time!
if Time.now() > #note.created_at + 3.days
flash[:danger] = 'Out of 3 days'
redirect_to note_path(#note)
end
end
end
I have a has_one association in my model with my user. What I'm trying to do here is simple but I'm having a hard time understanding whats wrong here. So since I have a has_one association with my model, in my mind I was simply thinking that if the user has already created the model associated with the has_one association if he tries accessing "localhost3000/model/new" I would redirect him to the edit page of this particular model. Here is what I have but its telling me its not working as intended. It's as if my if statement is not catching anything
class BusinessesController < ApplicationController
before_action :set_business, only: [:show, :edit, :update, :destroy]
def index
#businesses = Business.all
#buzzs = Buzz.all
end
def show
end
def new
if current_user.business.nil?
#business = current_user.build_business
else
render 'edit'
end
end
def edit
end
def create
#business = current_user.build_business(business_params)
if #business.save
redirect_to #business, notice: 'Business was successfully created.'
else
render "new"
end
end
This error does not make a lot of sense to me because it says its an error in the "new" controller which would have rendered it to the edit path thus not being nil
This is happening because you're not setting #business when redirecting to 'edit'. Try this:
def new
if current_user.business.nil?
#business = current_user.build_business
else
#business = current_user.business
render 'edit'
end
end
I'm building an app which consists on sharing résumés. I am using Devise gem. Each user is able to create only one résumé. I made the models and and their relations. Resume belongs_to User and User has_one 'Resume'.
After making the views, I wanted to test my app but I got the error: undefined methodbuild' for nil:NilClass`
Here is my ResumeController and my routes.rb
class ResumeController < ApplicationController
before_action :find_resume, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:show]
def show
# #resume = Resume.find_by(params[:id])
end
def new
#resume = current_user.resume.build
end
def create
#resume = current_user.resume.build(resume_params)
if #resume.save
redirect_to #resume, notice: "resume was successfully created"
else
render 'new'
end
end
def edit
end
def update
if #resume.update(pin_params)
redirect_to #resume, notice: "resume was successfully updated"
else
render 'edit'
end
end
def destroy
#resume.destroy
redirect_to root_path
end
private
def resume_params
params.require(:resume).permit(:title, :description)
end
def find_resume
#resume = resume.find(params[:id])
end
end
Routes.rb
Rails.application.routes.draw do
devise_for :users
resources :resume, except: [:index]
get 'static_pages/index'
root to: "static_pages#index"
end
I just want the user to be able to create only one Resume and then he will be able to share it.
Update: After following messanjah's answer there was another error coming from the _form.html.erb: undefined method resumes_path' for #<#<Class:0x00...>. Here is the gist with forms and model: goo.gl/XvW2LH So you can see all the files closely.
Without more knowledge of where the error is happening, I can only suggest some areas that might be suspect.
To build a has_one relationship, you must use the build_*association* constructor.
def new
#resume = current_user.build_resume
end
def create
#resume = current_user.build_resume(resume_params)
end