I have a ruby on rails app. I added date_field to the view for add/Edit dates. the problem is that I have problem for saving and updating new dates but it brings the date from variable to the form(Edit) and after editting a date and creatine a new date. it doesn't come back to the home page or have problem with saving. as I saw in the console params, my date variable(start) changed when the user select new date but it does not load the first page afterward and I got this error:
F, [2018-02-01T16:03:44.784113 #723] FATAL -- :
NoMethodError (undefined method `year' for nil:NilClass):
app/controllers/weeks_controller.rb:27:in `create'
here is my form page where it shows date to edit:
.form-group
= f.label :course
= #course.name
.form-group.form-inline
= f.label :start
= f.date_field :start, as: :date, value: f.object.try(:strftime,"%m/%d/%Y"), class: 'form-control'
//= f.date_select :start, {}, { :class => "form-control" }
.actions
= f.submit 'Save', :class => "btn btn-primary"
an the error says about controller. but I do not have any 'year' method in controller:
def create
#week = Week.new(week_params.merge(course_id: #course.id))
respond_to do |format|
if #week.save
format.html { redirect_to '/weeks', notice: 'was successfully created.' }
format.json { render action: 'show', status: :created, location: #week }
else
format.html { render action: 'new' }
format.json { render json: #week.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #week.update(week_params)
format.html { redirect_to '/weeks', notice: 'starting week was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #week.errors, status: :unprocessable_entity }
end
end
end
weeks_params defined as follows:
def week_params
params.require(:week).permit(:start, :course_id)
end
I have at first problem of showing date with date_select but now with date_field it shows that but I do not know where this error refers to. I would be thankful if any one would help me.
The model is:
class Week < ActiveRecord::Base
belongs_to :course
has_many :assessments
def select_name
start.strftime("Week of %b %d")
end
end
Related
Rails 3.2
In lib/model_extension.rb, I have:
module ModelExtension
def generate_model_id
self.id = "#{Time.now.to_f.to_s.gsub /\./, '_'}_#{self.class.name.underscore}" if id.blank?
end
def add_ticket_id_to_model(ticket_id)
self.ticket_id = ticket_id
end
end
In controllers/admin/lead_billings_controller.rb, I have:
def create
#lead_billing = LeadBilling.new(params[:lead_billing])
#lead_billing.generate_model_id
#lead_billing.add_ticket_id_to_model(ticket_id)
#ticket = Ticket.find(params[:ticket_id])
respond_to do |format|
if #lead_billing.save
format.html { redirect_to #ticket, notice: 'Lead billing was successfully created.' }
format.json { render json: #lead_billing, status: :created, location: #lead_billing }
else
format.html { render action: "new" }
format.json { render json: #lead_billing.errors, status: :unprocessable_entity }
end
end
end
In models/lead_billing.rb, here's what I have:
class LeadBilling < ActiveRecord::Base
include ModelExtension
attr_accessible :ticket_id, :pre_tax_total, :post_tax_total
belongs_to :ticket
end
In views/lead_billings/leadbillings.html.slim, I have:
- pre_tax_total = #ticket.lead_billing.pre_tax_total.to_f
- post_tax_total = #ticket.lead_billing.post_tax_total.to_f
table.table.ajax-table.show-mode
tr.head-row
th Pre-Tax Total
th Post-Tax Total
tr
td = number_to_currency(pre_tax_total, :unit => "$")
td = number_to_currency(post_tax_total, :unit => "$")
- if lead_billing.status == 'entered'
= form_for(LeadBilling.new, url: lead_billing_path) do |f|
= hidden_field_tag :ticket_update_type, "complete_and_generate_lead_invoices"
= hidden_field_tag :ticket_id, #ticket.id
.form-horizontal-column.customer-info
.form-group
.form-horizontal-column.customer-info
.actions = f.submit 'Complete And Generate Invoices'
.clear
When I fill and submit the form, I get an error message, and this is what I have in the log file:
Started POST "/admin/lead_billings" for 162.17.182.1 at 2016-12-01 18:36:01 +0000
Processing by Admin::LeadBillingsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "ticket_update_type"=>"save_lead_billing", "ticket_id"=>"1473264666_2463856_ticket", "lead_billing"=>{"pre_tax_total"=>"100", "post_tax_total"=>"120"}, "commit"=>"Save Lead Billing Details"}
.......................
NameError (undefined local variable or method `ticket_id' for #<Admin::LeadBillingsController:0x0000000919e368>):
app/controllers/admin/lead_billings_controller.rb:44:in `create'
app/middleware/catch_json_parse_errors.rb:8:in `call'
I double checked the lead_billings table and it has the ticket_id column.
Any ideas?
in your controller #lead_billing.add_ticket_id_to_model(ticket_id) you missed to either set ticket_id or just say params[:ticket_id] on that line
I ran into a very strange bug in my Rails app: Whenever I try to submit a form I get the error No route matches [PATCH] "/business_cases" (in this case). But after clicking the "Back"-Button in my Browser and filling out + submitting again, it works fine. As this error appears not only in this specific model but almost every other I'm quite lost.. Here is the example.
routes:
business_cases GET /business_cases(.:format) business_cases#index
POST /business_cases(.:format) business_cases#create
new_business_case GET /business_cases/new(.:format) business_cases#new
edit_business_case GET /business_cases/:id/edit(.:format) business_cases#edit
business_case GET /business_cases/:id(.:format) business_cases#show
PATCH /business_cases/:id(.:format) business_cases#update
PUT /business_cases/:id(.:format) business_cases#update
DELETE /business_cases/:id(.:format) business_cases#destroy
routes.rb:
root 'dashboards#index'
get "index" => "pages#index"
resources :users, :orders, :sales, :business_cases
controller (FYI: I use cancancan):
class BusinessCasesController < ApplicationController
load_and_authorize_resource
before_action :authenticate_user!
def index
end
def show
end
def new
end
def edit
end
def create
respond_to do |format|
if #business_case.save
format.html { redirect_to business_cases_url, notice: 'Business case was successfully created.' }
format.json { render :show, status: :created, location: #business_case }
else
format.html { render :new }
format.json { render json: #business_case.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #business_case.update(business_case_params)
format.html { redirect_to business_cases_url, notice: 'Business case was successfully updated.' }
format.json { render :show, status: :ok, location: #business_case }
else
format.html { render :edit }
format.json { render json: #business_case.errors, status: :unprocessable_entity }
end
end
end
def destroy
#business_case.destroy
respond_to do |format|
format.html { redirect_to business_cases_url, notice: 'Business case was successfully destroyed.' }
format.json { head :no_content }
end
end
model:
class BusinessCase < ActiveRecord::Base
validates :name, :description, :presence => true
end
form (shorten):
<%= form_for #business_case, html: { multipart: true } do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: "X-Sell" %>
</div>
<% end %>
I don't know how update subject of teacher and delete notation (record) from subscription table
teacher_controller
def update
#subject = Subject.where("id=?", params[:subject_id]).first
#teacher.subjects << #subject
respond_to do |format|
if #teacher.update(teacher_params)
format.html { redirect_to #teacher, notice: 'Teacher was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #teacher.errors, status: :unprocessable_entity }
end
end
end
And what write in bellow code, that delete teacher and fields in subscription table - teacher_id and subject_id?
def destroy
#teacher.destroy
respond_to do |format|
format.html { redirect_to teachers_url }
format.json { head :no_content }
end
end
teachers/_form.html.erb (edit form)
<%= form_for(#teacher,:html => { class: 'login-form' }) do |f| %>
<%= select_tag "subject_id", options_from_collection_for_select(#subjects, "id", "name") %>
<% form %>
routes.rb
resources :teachers do
resources :subjects
end
You have defined #subject in your controller and using #subjects in the select_tag
Change it to
<%= select_tag "subject_id", options_from_collection_for_select(#subject, "id", "name") %>
I have a form with a drop down generated from a collection_select. The collection_select starts off blank and then when the user selects a date from the date field, the collection_select is updated with values for the date that is chosen.
I'm trying to show a nice error message if the form is submitted without a value chosen in my dropdown. Currently i'm getting this error: undefined methodmap' for nil:NilClass`
How can i make it so that if the user doesn't select a value in the dropdown, I can show them a nice error message?
View
<%= form_for(#arrangement) do |f| %>
<div class="control-group">
<%= f.label :date, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :date, :class => 'input-large datepicker' %>
</div>
</div>
<div class="control-group">
<%= f.label :timeslot, :class => 'control-label' %>
<div class="controls">
<%= collection_select(:arrangement, :timeslot_id, #available_timeslots, :id, :timeslot_spelled) %>
</div>
</div>
<% end %>
Controller
# GET /arrangements/new
# GET /arrangements/new.json
def new
date = Time.now.to_date.to_s(:db)
#arrangement = Arrangement.new
#available_timeslots = Timeslot.where("location_id = ? AND date(timeslot) = ? AND arrangement_id is null", current_user.user_details.location_id, date).order('timeslot ASC')
respond_to do |format|
format.html # new.html.erb
format.json { render json: #arrangement }
end
end
# POST /arrangements
# POST /arrangements.json
def create
#arrangement = Arrangement.new(params[:arrangement])
respond_to do |format|
if #arrangement.save
# update the timslot record with the arrangement id
if #timeslot = Timeslot.update(#arrangement.timeslot_id, :arrangement_id => #arrangement.id)
format.html { redirect_to #arrangement, notice: 'Arrangement was successfully created.' }
format.json { render json: #arrangement, status: :created, location: #arrangement }
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
end
end
The error given is referring to #available_timeslots being empty when trying to save the form
I would try something like this.
def new
#available_timeslots = ...
if #available_timeslots.count > 0
flash.now[:error] = "nil errrraaarrr"
end
...
end
In view
<div class="controls">
<%- if #available_timeslots.count > 0 %>
<%= collection_select(:arrangement, :timeslot_id, #available_timeslots, :id, :timeslot_spelled) %>
<% else %>
<%= flash.now[:error] %>
<% end %>
</div>
#available_timeslots is nil. Make sure it's set with an array of available timeslots to avoid this error message.
The trick was to add #available_timeslots = [] in the else clause
def create
#arrangement = Arrangement.new(params[:arrangement])
respond_to do |format|
if #arrangement.save
# update the timslot record with the arrangement id
if #timeslot = Timeslot.update(#arrangement.timeslot_id, :arrangement_id => #arrangement.id)
format.html { redirect_to #arrangement, notice: 'Arrangement was successfully created.' }
format.json { render json: #arrangement, status: :created, location: #arrangement }
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
end
end
I'm trying to pass a project id from a session in a hidden field on a task form, so that when the task is created, it has the id of the project that it is assigned to. I've done this before fine, and have even tried copying over the code that I used from when it worked, but changing names and I'm getting errors no matter what I do - if anyone could help point out where I'm going wrong, it would be much appreciated, thanks!
The error I'm getting with this configuration is: "unknown attribute: project_id"
View Code (tasks/_form):
<%= form_for(#task) do |f| %>
<div class="field">
<%= f.hidden_field :project_id, :value => session[:project_id] %>
</div>
...
<% end %>
Model Code (task):
attr_accessible :project_id
belongs_to :project
Controller code (tasks_controller):
def new
#task = Task.new
#project_id = session[:project_id]
respond_to do |format|
format.html # new.html.erb
format.json { render json: #task }
end
end
def create
project_id = session[:project_id]
#task = Task.new(params[:task])
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render json: #task, status: :created, location: #task }
else
format.html { render action: "new" }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
Here's the application trace - it is pointing to line 46, which in my code is the '#task = Task.new(params[:task])' line in the create action...?
app/controllers/tasks_controller.rb:46:in `new'
app/controllers/tasks_controller.rb:46:in `create'
Does the Task model have a project_id column?