简体   繁体   中英

Ruby on rails. Set a form field as nil/blank from controller

I am trying to let users RELIST 'gigs' that they have previously posted by duplicating the old one and saving it with a new ID. At the moment everything duplicates fine and can be saved but I need one of the fields (datetime) to be blank as it is vital that they manually set this. I do not know how to set this as nil/blank from the controller.

Relist link:

<%= link_to t('gigs.show.relist'), gig_relist_path(:gig_id => @gig.id), class: 'gig-edit-dash' %>

Gig controller:

def relist
  @oldgig = Gig.find(params[:gig_id])
  @gig = @oldgig.dup
end

Form:

<div class="create-form">
<%= simple_form_for @gig do |form| %>
<div class="create-title">
 <%= form.input :title, label: t('gig.title'), placeholder: t('placeholder.title') %></div>
 <div class="create-location">
 <%= form.input :location, label: t('gig.location'), placeholder: t('placeholder.location') %></div>
 <div class="create-genre">
 <ul></ul>
 <%= form.label :genres %>
<%= select_tag "choose_genres", options_from_collection_for_select(Genre.all, 'id', 'name',@gig.genres.map{ |j| j.id }), :multiple => true %> </div>

 <div class="create-description">
 <%= form.input :description, as: :text, label: t('gig.description'), placeholder: t('placeholder.description') %></div>
 <div class="create-date">
 <%= form.input :date, label: t('gig.date'), placeholder: t('placeholder.date') %></div>
 <div class="create-salary">

 <div class="salary-input">
 <p class="salary-title"> <%= t('gig.salary') %> </p>
   <%= form.input :salary_currency, label: false,
   collection: ["€", "£", "$" ], prompt: "Choose one" %>
 <%= form.input :salary, label: false, placeholder: t('placeholder.salary') %>
 </div>
 </div>

Setting the default value to nil/blank in the form doesn't work as a value is being passed.

@gig.date = nil 

doesn't work either, this sets the time to the current time.

Oh dear, I just figured this out about 2 minutes after posting the question. I'll leave the answer rather than deleting the post just in case someone else finds it useful.

I set @gig.date = nil in the controller after @gig = @oldgig.dup

Then added , :include_blank => true to the form field in the view.

The reasoning is pretty self explanatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM