I would like to switch params[:starts_at] and params[:ends_at] if the user passes an 'end_at' time before the 'start_at'
here's what i did but i don't think it's working
def create
if params[:starts_at] > params[:ends_at]
params[:starts_at],params[:ends_at] = params[:ends_at],params[:starts_at]
end
if !booking.save
report_sentry_errors(booking)
render json: booking.errors.messages, status: :unprocessable_entity
else
render 'show', status: :created
end
end
request exapmle :
starts_at: "2022-10-09T10:30:00.000Z"
ends_at: "2022-10-09T09:00:00.000Z"
The logic was pretty correct, I had to change the params with the booking
def create
if booking.starts_at > booking.ends_at
booking.starts_at,booking.ends_at = booking.ends_at,booking.starts_at
end
if !booking.save
report_sentry_errors(booking)
render json: booking.errors.messages, status: :unprocessable_entity
else
render 'show', status: :created
end
end
Related
I have two models: Activity and LocationActivity.
Activity can have a kind of place, this place can be room or location and this information I keep in my table location_activities which has a foreign key activity_id.
I need that when creating an activity and choose the type of place I can save the id of the activity in activity_id of my table location_activities
def create
#activity = Activity.new(activity_params)
if params[:activity][:type_place_activity] == RoomEvent.name
room_event = RoomEvent.find(params[:activity][:room_event][:id])
location_activity = LocationActivity.new
location_activity.activity_place = room_event
end
location_activity.activity_id = #activity
if #activity.save
render json: {status: 'created', message: 'Activity save'}
else
render json: #activity.errors, status: :unprocessable_entity
end
end
I have this code but the location_activities record is not saved in my database
How can i solve this?
It is not saved because you only initialize it, but never save. Also you try to pass #activtity.id as foreign key before it is saved, in this moment #activity has no id. Should be:
def create
#activity = Activity.new(activity_params)
if #activity.save
add_location(#activity)
render json: {status: 'created', message: 'Activity save'}
else
render json: #activity.errors, status: :unprocessable_entity
end
end
private
def add_location(activity)
return if params[:activity][:type_place_activity] != RoomEvent.name
room_event = RoomEvent.find(params[:activity][:room_event][:id])
activity.location_activities.create(activity_place: room_event)
end
Although Vasilisa's answer will solve the issue, I don't think that logic belongs in the controller. I would change the ActivitiesController like this:
def create
#activity = Activity.new(activity_params)
if #activity.save_with_location_activities(params[:activity][:room_event][:id])
render json: { status: 'created', message: 'Activity save' }
else
render json: #activity.errors, status: :unprocessable_entity
end
end
And then add the save_with_location_activities method to the Activity model like this:
class Activity
def save_with_location_activities(room_event_id)
room_event = RoomEvent.find(room_event_id)
if room_event && self.valid?
save
LocationActivity.create(activity: self, activity_place: room_event)
else
return false
end
end
end
I'm trying to render some specific json based on whether the destroy worked or not. However, the code gets past the #attachment.destroy... then throws an exception when it tries to render the json. Not sure what's going on here.
def delete
#attachment = Attachment.find(params[:attachment_id])
if #attachment.destroy
render json: {
status: 200,
message: MESSAGES_SUCCESS
}
else
render json: {
status: 422,
message: MESSAGES_FAILED
}
end
end
Destroy attachment and then check if there are any errors.
def delete
#attachment = Attachment.find(params[:attachment_id])
#attachment.destroy
if #attachment.errors.any?
render json: {
status: :unprocessable_entity, # 422
message: MESSAGES_FAILED
}
else
render json: {
status: :ok, # 200
message: MESSAGES_SUCCESS
}
end
end
I have a RoR app with postgresql as database
Currently, I have this code:
def return_mssg_as_json
id = params[:id].to_i
#message = Message.find(id)
render json: #message
end
and it returns this JSON:
{"id":13,"created_at":"2017-10-27T19:03:52.196Z","updated_at":"2017-10-27T19:03:52.196Z","text":"ASDF"}
How can I make it return only {"text":"ASDF"}
A few things here...
First, you don't need to do to_i here:
def return_mssg_as_json
id = params[:id].to_i
#message = Message.find(id)
render json: #message
end
You can just do:
def return_mssg_as_json
#message = Message.find(params[:id])
render json: #message
end
Now, if there's any chance that you might receive an id that doesn't have a matching record, you might want to do something like:
def return_mssg_as_json
begin
#message = Message.find(params[:id])
render json: #message
rescue ActiveRecord::RecordNotFound
render json: {errors: :not_found}, status: :not_found
end
end
Otherwise, if your record doesn't exist, you'll have an unhandled critical error. Instead, however, you could do:
def return_mssg_as_json
if #message = Message.find_by(id: params[:id])
render json: #message
else
render json: {errors: :not_found}, status: :not_found
end
end
Now, back to your main question, you could do (as in the accepted answer)
def return_mssg_as_json
if #message = Message.find_by(id: params[:id])
render json: { message: #message[:text] }
else
render json: {errors: :not_found}, status: :not_found
end
end
Although, to save yourself three characters, it seems you could do:
def return_mssg_as_json
if #message = Message.find_by(id: params[:id])
render json: { message: #message.text }
else
render json: {errors: :not_found}, status: :not_found
end
end
Which is a great answer! But, in the future, if you want to return multiple values from #message (for example), you could do something like (assuming #message responds to foo and bar):
def return_mssg_as_json
if #message = Message.find_by(id: params[:id])
render json: #message.attributes.with_indifferent_access.slice(:text, :foo, :bar)
else
render json: {errors: :not_found}, status: :not_found
end
end
This bit:
#message.attributes
converts the record into a hash of its values. This bit:
.with_indifferent_access
allows you to select key-value pairs using symbols instead of strings. And this bit:
.slice(:text, :foo, :bar)
specifies which key-value pairs to return. So, this would return something along the lines of (in JSON format, naturally):
{text: 'blah', foo: 'blah', bar: 'blah'}
Now, in your example, you didn't want text, you wanted message. So, you could do:
def return_mssg_as_json
if #message = Message.find_by(id: params[:id])
attr = #message.attributes.with_indifferent_access
render json: attr.slice(:foo, :bar).merge!(message: attr[:text])
else
render json: {errors: :not_found}, status: :not_found
end
end
Which would return something along the lines of (in JSON format, naturally):
{message: 'blah', foo: 'blah', bar: 'blah'}
Simply use the following:
render json: { message: #message[:text] }
Also you can use jbuilder to achieve such behavior, check an example here https://richonrails.com/articles/getting-started-with-jbuilder
I am trying to render error messages if any of the condition fails. How can I pass error messages related to failed conditions
But it gives me AbstractController::DoubleRenderError error
def create
if #current_wbp_user && params[:user_id] && params[:note_id] && params[:comment] && params[:hashtag]
user = User.find_by(id: params[:user_id])
if user.present?
if user.access_code != #current_wbp_user.access_code
render json: {errors: "User not associated with this wbp user"}, status: :unprocessable_entity
end
note = Note.find_by(id: params[:note_id])
if note.present?
if note.user_id != user.id
render json: {errors: "Invalid note for this user"}, status: :unprocessable_entity
end
else
render json: {errors: "Note not found"}, status: :unprocessable_entity
end
else
render json: {errors: "User not found"}, status: :unprocessable_entity
end
#comment = #current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
if #comment.save
render json: {success: true}, status: :ok
else
render json: {errors: "Comment could not be created"}, status: :unprocessable_entity
end
else
render json: {errors: "Insufficient Information"}, status: :unprocessable_entity
end
end
You need to add and return to every render in this block
if user.present?
...
end
to exit function. Example:
render json: {errors: "User not associated with this wbp user"}, status: :unprocessable_entity and return
No duplicate status codes and some refactoring, you can do some of validations in filters in this way your methods will look skinny and good
def create
sucess = false
if #current_wbp_user && [:user_id, :note_id, :comment, :hashtag].all? {|s| params.key? s}
user = User.find_by(id: params[:user_id])
if user.present?
if user.access_code != #current_wbp_user.access_code
message = "User not associated with this wbp user"
end
note = Note.find_by(id: params[:note_id])
if note.present?
if note.user_id != user.id
message = "Invalid note for this user"
end
else
message = "Note not found"
end
else
message = "User not found"
end
#comment = #current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
if #comment.save
sucess = true
else
message = "Comment could not be created"
end
else
message = "Insufficient Information"
end
if sucess
render json: {success: true}, status: :ok
else
render json: {errors: message}, status: :unprocessable_entity
end
end
def create
error_msgs = Array.new
if #current_wbp_user && params[:user_id].present? && params[:note_id].present?
user = User.find_by(id: params[:user_id])
if user.present?
if user.access_code != #current_wbp_user.access_code
error_msgs << "User not associated with this wbp user"
end
note = Note.find_by(id: params[:note_id])
if note.present?
if note.user_id != user.id
error_msgs << "Invalid note for this user"
end
else
error_msgs << "Note not found"
end
else
error_msgs << "User not found"
end
if params[:comment].present? && params[:hashtag].present?
#comment = #current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
if #comment.save
render json: {success: true}, status: :ok
return
else
error_msgs << "Comment could not be created"
end
end
else
error_msgs << "Insufficient Information"
end
if error_msgs.present?
render json: {errors: error_msgs}, status: :unprocessable_entity
end
end
I need to save items separately coming from a form of a text field, but my code is saving these items duplicate form.
My controller
def create
#answer_option = AnswerOption.break_options(answer_option_params)
#answer_option = AnswerOption.new(answer_option_params)
respond_to do |format|
if #answer_option.save
format.html { redirect_to #answer_option, notice: 'Answer option was successfully created.' }
format.json { render :show, status: :created, location: #answer_option }
else
format.html { render :new }
format.json { render json: #answer_option.errors, status: :unprocessable_entity }
end
end
end
My model
class AnswerOption < ActiveRecord::Base
belongs_to :question
def self.break_options(var)
ugly_answers = var[:content].split /[\r\n]+/
ugly_answers.each do |answer|
AnswerOption.create!(content: answer)
end
end
end
Thanks!
def create
#answer_option = AnswerOption.break_options(answer_option_params)
end