I have a problem with my re-edition. Let me explain it : When I edit and update an object current value is ok in my show view. But if i re-edit current value isn't the last selected.
Problem is only with checkbox or select.
My form
<%= f.fields_for :situations do |s| %>
<p><label for="plage">Plage</label>
<%= s.select :plage?, ["", "oui","non"] %> à <%= s.select :distanceplage?, ["", "moins de 1", "2","3", "4", "5 et plus"] %> km</p>
<% end %>
controller
def edit
end
def update
#camping = Camping.find(params[:id])
respond_to do |format|
if #camping.update(camping_params)
format.html { redirect_to #camping, notice: 'Camping was successfully updated.' }
format.json { render :show, status: :ok, location: #camping }
else
format.html { render :edit }
format.json { render json: #camping.errors, status: :unprocessable_entity }
end
end
end
How i can fix it ? Thanks for your help !
Ok so after some search solution is this :
:include_blank => true
so my view looks like this
<p><label for="plage">Plage</label>
<%= s.select :plage, ["oui","non"], :include_blank => true %> à <%= s.text_field :distanceplage %> km</p>
Related
I'm trying to update the number of holiday days an employee has when they book a holiday. I'm trying to save the employee from the model that doesn't seem to be working, the holiday creates perfectly otherwise. I had it working before but must've changed something by mistake as no matter what I do I cant get it back working. Any Ideas?
Holidays Controller
def create
#holiday = Holiday.new(holiday_params)
#holiday.employee_id = current_employee.id
if(Holiday.pastHol(#holiday))
if(Holiday.h_authorize(current_employee, #holiday))
if(Holiday.update_holidays(current_employee,#holiday))
respond_to do |format|
if #holiday.save
format.html { redirect_to action: "index", notice: 'holiday accecpted' }
format.json { render :show, status: :created, location: #holiday }
end
end
else
respond_to do |format|
format.html { redirect_to action: "index", notice: 'holiday declined' }
format.json { render json: #holiday.errors, status: :unprocessable_entity }
end
end
else
respond_to do |format|
format.html { redirect_to action: "index", notice: "Already on Hols" }
format.json { render json: #holiday.errors, status: :unprocessable_entity }
end
end
else
respond_to do |format|
format.html { redirect_to action: "index", notice: "Date error" }
format.json { render json: #holiday.errors, status: :unprocessable_entity }
end
end
end
Holiday Model
scope :update_holidays, lambda{ |q| where("amount > ?", q) }
scope :proj_authorize, lambda{ |q| where("amount > ?", q) }
def self.update_holidays(employee, holiday)
employee.DaysHolidays == employee.DaysHolidays - (holiday.endDate - holiday.startDate ) - 1
if (employee.DaysHolidays > 0)
employee.save
return true
else
return false
end
end
Holiday Index
<%= form_for(#holiday) do |f| %>
<%= errors_for(#holiday) %>
<h3>Holiday Request</h3>
<%= current_employee.name %> , you have <%= current_employee.DaysHolidays %> holiday days remaining.<br><br>
<p id="notice"><%= notice %></p>
<div class="field">
<%= f.label :startDate %><br>
<%= f.text_field :startDate , id: :datepicker, placeholder: "Start Date" %>
</div>
<div class="field">
<%= f.label :endDate %><br>
<%= f.text_field :endDate , id: :datepicker1, placeholder: "End Date" %>
<div class="actions">
<%= f.submit 'Request Holiday', :class => 'btn' %>
</div>
<% end %>
</form>
</div>
I've noticed that you've already found the answer to your question, but I really wanted to refactor a bit your controller. While I highly advise you move your if cases to a service class or underlying approach probably should be different but without other information, I can do so much. Here is a slight refactor while keeping everything in the controller:
def create
#holiday = Holiday.new(holiday_params)
#holiday.employee_id = current_employee.id
notice =
if(Holiday.pastHol(#holiday))
if(Holiday.h_authorize(current_employee, #holiday))
if(Holiday.update_holidays(current_employee, #holiday))
"holiday accepted"
else
"holiday declined"
end
else
"Already on Hols"
end
else
"Date error"
end
respond_to do |format|
format.html { redirect_to action: "index", notice }
if #holiday.save
format.json { render :show, status: :created, location: #holiday }
else
format.json { render json: #holiday.errors, status: :unprocessable_entity }
end
end
end
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 understand how to implement a single has_many association using simple_form, but how do you assign an additional association from another model object?
In my code, I'm creating model object #opportunity. I'm currently assigning a company_id, but also need to assign a 'user_id.
#opportunity _form.html.erb
<% if user_signed_in? %>
<%= simple_form_for([#company, #company.opportunities.build], html: {class: "form-inline"}) do |f| %>
<%= f.error_notification %>
<%= f.input :description, label: false, placeholder: 'Create an opportunity', input_html: { class: "span4" } %>
<%= f.submit 'Submit', class: 'btn btn-small'%>
<% end %>
<% else %>
<%= link_to "Create an Account", new_user_registration_path %>
to contribute
<% end %>
opportunity_controller.rb
def create
#company = Company.find(params[:company_id])
#opportunity = #company.opportunities.create(params[:opportunity])
respond_to do |format|
if #opportunity.save
format.html { redirect_to company_path(#company), notice: 'Opportunity was successfully created.' }
format.json { render json: #opportunity, status: :created, location: #opportunity }
else
format.html { render action: "new" }
format.json { render json: #opportunity.errors, status: :unprocessable_entity }
end
end
end
Assuming that your user is logged in, you can change your controller action to the following:
def create
#company = Company.find(params[:company_id])
#opportunity = #company.opportunities.new(params[:opportunity]) # new instead of create
#opportunity.user = current_user # new
respond_to do |format|
if #opportunity.save
format.html { redirect_to company_path(#company), notice: 'Opportunity was successfully created.' }
format.json { render json: #opportunity, status: :created, location: #opportunity }
else
format.html { render action: "new" }
format.json { render json: #opportunity.errors, status: :unprocessable_entity }
end
end
end
I am trying to submit a form and getting this error: Couldn't find Event without an ID
Here is the controller:
def create
#event = Event.find(params[:event_id])
#event_sponsorship = EventSponsorship.new(params[:event_sponsorship])
#event_sponsorship.sponsor_id = current_user.id
#event_sponsorship.event_id = #event
respond_to do |format|
if #event_sponsorship.save
format.html { redirect_to #event_sponsorship, notice: 'Event sponsorship was successfully created.' }
format.json { render json: #event_sponsorship, status: :created, location: #event_sponsorship }
else
format.html { render action: "new" }
format.json { render json: #event_sponsorship.errors, status: :unprocessable_entity }
end
end
end
Here is the form:
<%= simple_form_for(#event_sponsorship) do |f| %>
<%= f.error_notification %>
<div class="signin">
<%= f.hidden_field :event_id, value: #event %>
<%= f.hidden_field :sponsor_id, value: current_user.id %>
<%= f.button :submit, :class => "btn btn-success btn-block", value: "Yes" %>
</div>
<% end %>
In the create method the event_id should be found from the URL. Where am I going wrong?
Post your error as seen in your server console, you'll see that your param 'event_id' is a sub key of (I guess) 'event_sponsorship'
Try this:
#event = Event.find(params[:event_sponsorship][:event_id])
And it's even possible that you'll need to use this code:
#event = Event.find(params[:event_sponsorship].delete(:event_id))
That would remove the 'event_id' fron your params, and allow EventSponsorship.new to work without error
Okay so my associations are:
Outlet -> has_many :monitorings
Monitoring -> belongs_to :outlet
My Routes:
resources :outlets do
resources :monitorings
end
View:
<%= link_to new_outlet_monitoring_path(#outlet) %>
When I click the link, the logs show that the outlet_id is passed as a parameter to the new page correctly.
But when saving the monitoring record, the outlet_id becomes nil.
Any help?
UPDATE:
# views/monitorings/_form.html.erb
<%= form_for(#monitoring) do |f| %>
<h2>Type of Monitoring</h2>
<fieldset data-role="controlgroup" >
<div class="radio-group">
<%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %>
<%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %>
<%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %>
</div>
</fieldset>
<hr>
<%= f.submit "Next Step" %>
<% end %>
And the controller:
# controllers/monitoring_controller.rb
def new
#monitoring = Monitoring.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #monitoring }
end
end
def create
#monitoring = Monitoring.new(params[:monitoring])
respond_to do |format|
if #monitoring.save
format.html { redirect_to #monitoring, notice: 'Monitoring was successfully created.' }
format.json { render json: #monitoring, status: :created, location: #monitoring }
else
format.html { render action: "new" }
format.json { render json: #monitoring.errors, status: :unprocessable_entity }
end
end
end
This is most likely an issue with the way you are creating the new monitoring record. Can we see your form and your create controller action?