ruby on rails - How to edit a model with its relations with form_for? -


i have problem using form_for-tag in rails.

i have following models:

holidays

   id      |   name ___________|_____________    123     |   "asdf"    345     |   "foo"    757     |   "bar" 

holidates

   id      |   date ___________|_____________    223     |   1998-03-11    445     |   1999-06-21    557     |   2009-09-20 

countries

   id      |   name      |   country_code ___________|_____________|________________    667     |   "italia"  |   "it"    987     |   "france"  |   "fr"    999     |   "austria" |   "au" 

r_countries

   id      |   country_id|   holiday_id ___________|_____________|________________    463     |   667       |   123    574     |   987       |   345    687     |   999       |   757 

r_holidates

   id      |   holidate_id|   holiday_id ___________|______________|________________    142     |   123        |   223    253     |   345        |   445    362     |   757        |   557 

i want use 1 form create holiday, name, date , country, _form:

<%= form_for(@holiday) |f| %>   <% if @holiday.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@holiday.errors.count, "error") %> prohibited timespan being saved:</h2>       <ul>       <% @holiday.errors.full_messages.each |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>     <div class="field">     <%= f.label :name %><br>     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :holidate %><br>     <%= f.date_select :holidate %>   </div>   <div class="field">     <%= f.label :country_code %><br>     <%= f.date_select :country_code %>   </div>   <div class="field">     <%= f.label :country_name %><br>     <%= f.text_field :country_name %>   </div>    <div class="actions">    <%= f.submit %>   </div> <% end %> 

but, sure, when executing script, error:

nomethoderror in holidays#edit showing c:/xampp/htdocs/fluxcapacitor/app/views/holidays/_form.html.erb line #20 raised: undefined method `holidate' #


edit: here design example, how should look: layout


edit2: have populated database seeds-file, used script like:

from = date.civil(2000,1,1)                                                          = date.civil(2000,12,31)                                                              holidays.between(from, to, :any ).each |date|                                         lname = date[:name]     ldate = date[:date]     lregion = date[:regions]     holiday.find_or_create_by(name:  lname)             #holiday created     holidate.find_or_create_by(date: ldate)                                                                                  #creates holidate     rholidate.find_or_create_by(holidate_id: holidate.find_by(date: ldate).id, holiday_id: holiday.find_by(name: lname).id)  #this should create relation between holiday , it's date     ####     lregion.each |x|         country.create(country_code: x, name: x)         if  !(rcountry.exists?(country_id: country.find_by(country_code: x).id, holiday_id: holiday.find_by(name: lname).id))             rcountry.create(country_id: country.find_by(country_code: x).id, holiday_id: holiday.find_by(name: lname).id)         end     end  end  

i not sure code, form can looks like(refine needs):

<%= form_for(@holiday) |f| %>   ........some code errors here....    <%= fields_for :holydate, @holyday.holydate |holydate| %>    <%= holydate.date_select :holidate  %>   <% end %>    ........some other code here....     <%= f.submit %> <% end %> 

or there alternative way using tags , calling them in create/update methods:

  <div class="field">     <%= label_tag 'date' %><br />     <%= date_field_tag 'holidate' %>   </div>   <div class="field">     <%= label_tag 'countrycode' %><br />     <%= text_field_tag :country_code %>   </div>   <div class="field">     <%= label_tag 'countryname' %><br />     <%= text_field_tag 'country_name' %>   </div> 

here controller method:

  def create     @holiday = holiday.new(holiday_params)     e=date.civil(d[:year].to_i,d[:month].to_i,d[:day].to_i)     holidate=holidate.where(:date => e).first_or_create     rholidate.where(:holidate_id => holidate.id, :holiday_id => @holiday.id ).first_or_create     country=country.where(:name => params[country_name], :country_code => params[country_code] ).first_or_create     rcountry.where(:country_id => country.id, :holiday_id => @holiday.id ).first_or_create     respond_to |format|       if @holiday.save         format.html { redirect_to @holiday, notice: 'holiday created.' }         format.json { render action: 'show', status: :created, location: @holiday }       else         format.html { render action: 'new' }         format.json { render json: @holiday.errors, status: :unprocessable_entity }       end     end   end 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -