i have two tables in my application
rfp.rb
has_many :rfp_hors
attr_accessible :rfp_hors_attributes
accepts_nested_attributes_for :rfp_hors, :allow_destroy => true
rfp_hor.rb
attr_accessible :numberofmenu_est_hours,
:numberofmenu_act_hours,
:browser_est_hours,
:browser_act_hours,
:numberofpage_est_hours,
:numberofpage_act_hours,
:rfp_id
belongs_to :rfp
when i submit rfp_hors the parameter shows as follows in console
Parameters: {"rfp_hor"=>{"ecommerce_est_hours"=>"7", "rfp_id"=>"13", "designcomplexity_est_hours"=>"3", "browser_est_hours"=>"4", "framworks_est_hours"=>"5", "cms_est_hours"=>"6"}, "utf8"=>"✓", "commit"=>"Create Rfp hor", "authenticity_token"=>"XXgQlufpBP2lvcde/EiFIx93aM5Ov47MNFqsCkLun2Y="}
and controller
rfps.rb
def show
#rfp = Rfp.find(params[:id])
#rfp_hor = RfpHor.new
end
rfp_hors.rb
def create
#rfp_hor = RfpHor.create(params[:rfp_hor])
respond_to do |format|
if #rfp_hor.save
format.html { redirect_to rfp_url(#rfp_hor.rfp_id), :notice => 'rfp hour was successfully created.' }
format.json { render :json => #rfp_hor, :status => :created, :location => #rfp_hor }
else
format.html { render :action => "new" }
format.json { render :json => #rfp_hor.errors, :status => :unprocessable_entity }
end
end
end
every thing saves fine in databse aceept rfp_id in rfp_hors
any help would be great thanks in advance
your problem is because you are initializing the variable #rfp_hor as new independent object in the rfps controller when would you initialize only the varbiale #rfp, you could try of this way:
def edit
#rfp = Rfp.find(params[:id])
end
on your update action of the same controller, you don't have to change nothing, and you can put this code in your form:
<%= form_for #rfp do |f| %>
<%= f.fields_for : rfp_hors do |item| %>
<%= item.field_one :field %>
<%= item.field_two :field %>
<% end %>
<% end %>
in this way you can to receive the params as nested form in the same controller in the update action and you can show the params of this mode:
Parameters: {"rfp"=>{"rfp_hors_attributes"=>{"ecommerce_est_hours"=>"7", "rfp_id"=>"13", "designcomplexity_est_hours"=>"3", "browser_est_hours"=>"4", "framworks_est_hours"=>"5", "cms_est_hours"=>"6"}}, "utf8"=>"✓", "commit"=>"Create Rfp hor", "authenticity_token"=>"XXgQlufpBP2lvcde/EiFIx93aM5Ov47MNFqsCkLun2Y="}
Related
I am trying to create a register for a school, I got to the point where I had to create the class lessons and save presences/absences in it.
I'm thinking about making appear a collection_check_boxes which gathers all the students who belong to the class we are recording the lesson for.
At this point the checkbox is properly displayed on the vista, but it doesn't fill the array student_id of Lesson when it's created.
Il controller
def new
#lesson = Lesson.new
#group_id = params[:group_id]
#students = Student.where(:group_id => #group_id)
end
def create
#lesson = Lesson.new(lesson_params)
respond_to do |format|
if #lesson.save
format.html { redirect_to #lesson, notice: 'Lesson was successfully created.' }
format.json { render :show, status: :created, location: #lesson }
else
format.html { render :new }
format.json { render json: #lesson.errors, status: :unprocessable_entity }
end
end
end
def lesson_params
params.require(:lesson).permit(:date, :starts_at, :finishes_at, :materials, :areas, :homework, :group_id, :student_id)
end
While the view that calls the check_boxes
<div class="field">
<%= form.collection_check_boxes :student_id, Student.where(:group_id => #group_id).order(name: :asc), :id, :name, {}, { multiple: true } %>
</div>
Can anybody help me, please?
Try this
<%= form.collection_check_boxes :student_ids, Student.where(:group_id => #group_id).order(name: :asc), :id, :name %>
ok, i solved
I had to declare the array between the parameters allowed in the controller
def lesson_params
params.require(:lesson).permit(:date, :starts_at, :finishes_at, :materials, :areas, :homework, :group_id, :student_id => [] )
end
i hope this can help someone else.
On form submission, it's telling me that it was successfully created but it's not showing any data that was submitted. The database is empty. It's showing "null" values and the same on the actual screen where I should be able to edit the data. Here's a screenshot
Update: I think the problem is that it's making a GET request but I don't know how to fix it. Here's a screen shot of my server doing a get when I clicked the submit
Here's the set up
In the index action of results_controller.rb, I have
def index
#results = Result.all
#blob = Sex.new //==#blob = Sex.new is the one I'm focussing on...
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #results }
end
end
In views/results/index, I have the form
<`%= form_for(#blob) do |f| %>`
<div class="field">
<b>1. solicitor exam was fixed?:</b><br/>
<%= f.label(:solicitorcurve, "it was cooked") %>
<%= f.radio_button(:solicitorcurve, "t") %> </br>
</div>
<div class="field">
<%= f.label(:solicitorcurve, "no it was ok") %>
<%= f.radio_button(:solicitorcurve, "f") %>
</div>
<div class="field">
<%= f.label(:draftingteach, "i give the teaching a grade of _ on a scale of 1 to 6") %>
<%= f.select:draftingteach, 1..6 %> </br>
</div>
In the create action of sexes_controller.rb i have
def create
#sex = Sex.new(params[:blob])
respond_to do |format|
if #sex.save
format.html { redirect_to(#sex, :notice => 'Sex was successfully created.') }
format.xml { render :xml => #sex, :status => :created, :location => #sex }
else
format.html { render :action => "new" }
format.xml { render :xml => #sex.errors, :status => :unprocessable_entity }
end
end
end
In models/sex.rb, there is nothing...
class Sex < ActiveRecord::Base
end
And this is the set up of the database
It looks like the issue is that you're retrieving params[:blob] when you should be looking at params[:sex]. form_for will create fields named after the class of the object. The instance variable name #blob you're using is arbitrary.
...
#sex = Sex.new(params[:sex])
...
This is a good argument for why you probably want to name instance variables for what they are. Less confusion.
I have a form that handles 2 models, Vehiculo and Poliza. This is how I have them set up right now:
class Vehiculo < ActiveRecord::Base
has_one :poliza
end
class Poliza < ActiveRecord::Base
belongs_to :vehiculo
end
The create method on Vehiculo looks like this:
def create
#vehiculo = Vehiculo.new(params[:vehiculo])
#polizadeseguro = Polizadeseguro.new(params[:poliza])
respond_to do |format|
if #vehiculo.save #&& #poliza.save
format.html { redirect_to(#vehiculo, :notice => 'Vehiculo was successfully created.') }
format.xml { render :xml => #vehiculo, :status => :created, :location => #vehiculo }
else
format.html { render :action => "new" }
format.xml { render :xml => #vehiculo.errors, :status => :unprocessable_entity }
end
end
The form on /vehiculos/new has a #fields_for part with the fields from poliza. When I submit the form, it is saving all the fields, but it is not assigning the just created id from vehiculo, to vehiculo_id on the Polizas table. After reading many questions about this online, It seems that it should save it "automagically" based on the relationships on the model. Is this true? If so, why isn't it working? If not, what do I need to add to the create method so I resolve this?
Thanks!
Update:
After updating the create method with json as output as suggested here is what I get:
{
"utf8"=>"✓",
"authenticity_token"=>"tEhNC4J17h+KvNgXv1LLkVyufQwU2uAT18P7msQxiqA=",
"vehiculo"=>{
"marca_id"=>"2",
"modelo_id"=>"4",
"color"=>"Blanco",
"ano"=>"2011",
"chassis"=>"123456789",
"placa"=>"G123456",
"cliente_id"=>"1",
"entaller"=>"0",
"vip"=>"0"
},
"poliza"=>{
"compania"=>"Comp1",
"numeropoliza"=>"736458",
"vencimiento(1i)"=>"2011",
"vencimiento(2i)"=>"9",
"vencimiento(3i)"=>"21"
}
}
That's the output, so at least it is getting the fields from the form, but it is not inserting them to the polizas table.
You need to make sure that your parent model accepts nested attributes for the child model:
class Vehiculo < ActiveRecord::Base
has_one :poliza
accepts_nested_attributes_for :poliza
end
Assuming your form is set up correctly, your params will look something like this:
params = {
:vehiculo => {
:field => "value",
:another_field => "value",
:poliza => {
:poliza_field => "poliza value"
}
}
}
So all you should need in your controller is:
def create
#vehiculo = Vehiculo.new(params[:vehiculo])
respond_to do |format|
if #vehiculo.save #&& #poliza.save
format.html { redirect_to(#vehiculo, :notice => 'Vehiculo was successfully created.') }
format.xml { render :xml => #vehiculo, :status => :created, :location => #vehiculo }
else
format.html { render :action => "new" }
format.xml { render :xml => #vehiculo.errors, :status => :unprocessable_entity }
end
end
end
[Update]
Here's what you'll need to have to have this all work.
As mentioned above, you need accepts_nested_attributes_for.
Next, make sure your new action is building the child.
class VehiculosController < ApplicationController
def new
#vehiculo = Vehiculo.new
#vehiculo.build_poliza
end
def create
vehiculo = Vehiculo.new(params[:vehiculo])
if vehiculo.save
redirect_to root_path, :notice => "Success"
else
redirect_to root_path, :alert => "Failure"
end
end
end
Finally, in your view, reference the child model using fields_for :child_model, as such:
<%= form_for #vehiculo do |f| %>
<p>Whatever Field: <%= f.text_field :whatever %></p>
<%= f.fields_for :poliza do |p| %>
<p>Polizo Field: <%= p.text_field :something %></p>
<% end %>
<% end %>
I'm a newbie with rad rails. I wanted to write at same time in two table from one form.
I have a table machine (with nom and role as column) and a table ipvfour (with machine_id and ip as column).
So I created in models the relation has-and-belongs-to-many.
But when I'm trying to add a new machine if failed with
unknown attribute: ip
I don't really understand why, can someone help me please ?
machine.controllers:
def create
#machine = Machine.new(params[:machine])
ipvfour = #machine.ip.create(params[:ip])
respond_to do |format|
if #machine.save && ipvfour.save
flash[:notice] = 'Machine was successfully created.'
format.html { redirect_to(#machine) }
format.xml { render :xml => #machine, :status => :created, :location => #machine }
else
format.html { render :action => "new" }
format.xml { render :xml => #machine.errors, :status => :unprocessable_entity }
end
end
end
new.html.erb (machine)
New machine
'form', :locals => { :f_machine => f_machine } %>
_form.html.erb (machine)
<% f_machine.fields_for :ip do |f_ip| %>
<%= render :partial => 'ipvfours/form', :locals => { :f_ip => f_ip } %>
<% end %>
_form.html.erb (ipvfours)
<%= f_ip.label :ip %><br />
<%= f_ip.text_field :ip %>
The page to add a machine is correclty displayed with all fields but it seems that write in db failed due to .... I hope that someone will be able ti help me.
Thanks in advance.
EDIT:
You can edit any model in any controller if you want. There's a magic trick called
accepts_nested_attributes_for (google it!)
Your code should look like:
In your Controller:
def new
# .... your code ....
# create empty machine
#machine = Machine.new
# add one empty ip
#machine.ipvfours.build
# .... your code ....
end
def create
# fill machine and ipvfours directly
#machine = Machine.new(params[:machine])
respond_to do |format|
if #machine.save
flash[:notice] = 'Machine was successfully created.'
format.html { redirect_to(#machine) }
format.xml { render :xml => #machine, :status => :created, :location => #machine }
else
format.html { render :action => "new" }
format.xml { render :xml => #machine.errors, :status => :unprocessable_entity }
end
end
end
In your view:
new.html.erb
<% form_for(#machine) do |f_machine| %>
<%= render :partial => 'form', :locals => { :f_machine => f_machine } %>
<%= f_machine.submit 'Create' %>
<% end %>
<%= link_to 'Back', machines_path %>
_form.html.erb (machine)
<%= f_machine.error_messages %>
<% f_machine.fields_for :ipvfours do |f_ip| %>
<%= render :partial => 'ipvfours/form', :locals => { :f_ip => f_ip } %>
<% end %>
_form.html.erb (ipvfours)
<%= f_ip.label :ip %>
<br />
<%= f_ip.text_field :ip %>
In your Model:
Machine model
class Machine < ActiveRecord::Base
has_many :ipvfours, :dependent => :destroy
accepts_nested_attributes_for :ipvfours
end
best regards
simon
I've read many posts about this issue but I never got this to work.
My model looks like this:
class Announcement < ActiveRecord::Base
validates_presence_of :title, :description
end
My controller's create method(only its relevant part) looks like this:
def create
respond_to do |format|
if #announcement.save
flash[:notice] = 'Announcement was successfully created.'
format.html { redirect_to(#announcement) }
format.xml { render :xml => #announcement, :status => :created, :location => #announcement }
else
#announcement = Announcement.new
#provinces = Province.all
#types = AnnouncementType.all
#categories = Tag.find_by_sql 'select * from tags where parent_id=0 order by name asc'
#subcategories= ''
format.html { render :action => "new" } #new_announcement_path
format.xml { render :xml => #announcement.errors, :status => :unprocessable_entity }
end
end
end
My form looks like this:
<% form_for(#announcement) do |f| %>
<%= error_messages_for 'announcement' %> <!--I've also treid f.error_messages-->
...
What am I doing wrong?
You are killing your error messages by creating a new announcement in your else statement.
#announcement = Announcement.new # should be removed
When you call #announcement.save it will store the errors in #announcement.errors. By calling #announcement = Announcement.new after this you are going back to a clean slate. So no errors will ever be displayed.