Having problems creating radio buttons in a form using has_one
Model
Class User
has_one :role
accepts_nested_attributes_for :role
Class Role
attr_accessible :name
belongs_to :user
Controller
#user = build_role
Form for radio buttons
<div class="field">
<% Role.offset(1).all.each do |role_fields| %>
<%= radio_button_tag "user[role_fields_id][]", role_fields.id, #user.role_id == role_fields.id %>
<%= role_fields.name %>
<% end %>
</div>
Getting the error
undefined method `role_id' for #<User id: nil, name: nil, created_at: nil, updated_at: nil>
I'm sure I set up the relationship in the controller, why is this not working?
I think this line:
<%= radio_button_tag "user[role_fields_id][]", role_fields.id, #user.role_id == role_fields.id %>
Should be corrected to this:
<%= radio_button_tag "user[role_fields_id][]", role_fields.id, #user.role.id == role_fields.id %>
I hope that helps.
Related
Having issues with nested attributes using fields_for. Anyone know what I am missing? I have checked many posts and RailsCasts and have not been able to figure it out.
Overview: I have a list of chart types (chart_type) that have one or more parameters (chart_param). Based on the chart type selected, I display the proper chart parameters and allow users to enter a value (chart_param_value). This is all from within my Indicators form.
Problem: The form submission is not retuning the nested attributes properly. The params being submitted look something like this "chart_param_values"=>{"value"=>"", "chart_param_id"=>"10024"}".
chart_type.rb
attr_accessible :multiple_indicators, :name, :chart_params_attributes
has_many :chart_params, :dependent => :destroy
accepts_nested_attributes_for :chart_params, :allow_destroy => true
def self.single_indicator
where(:multiple_indicators => false)
end
def self.multiple_indicators
where(:multiple_indicators => true)
end
chart_param.rb
attr_accessible :chart_type_id, :name, :required, :tooltip
belongs_to :chart_type
chart_param_value.rb
attr_accessible :chart_param_id, :indicator_id, :main_indicator_chart_id, :value
belongs_to :chart_param
#belongs_to :chart_type
belongs_to :indicator
indicator.rb
attr_accessible :chart_type_id, :description, :help_text, :key_name, :name, :display_screen, :any_details, :indicator_details_attributes, :chart_param_values_attributes
belongs_to :chart_type
has_many :chart_param_values, :dependent => :destroy
accepts_nested_attributes_for :chart_param_values
indicators/_form.html.erb
<%= form_for(#indicator) do |f| %>
...
<div class="field">
<%= f.label "Chart Type" %>
<%= f.select :chart_type_id, ChartType.single_indicator.collect { | ct | [ct.name, ct.id] }, {}, :onchange => "update_chart_params(this);" %>
</div>
<div id="Charts">
<% ChartType.single_indicator.each do |ct| %>
<div id="chart_type_params_<%= ct.id %>" <% if !#indicator.chart_type_id.eql?(ct.id) %> style="display: none" <% end %> >
<% ct.chart_params.each do |ctp| %>
<%= fields_for :chart_param_values do | builder | %>
<%= builder.hidden_field :chart_param_id, :value => ctp.id %>
<%= builder.label :value, ctp.required ? "* #{ctp.name}" : ctp.name %>
<%= builder.text_field :value %><br />
<% end %>
<% end %>
</div>
<% end %>
</div>
<script language="javascript">
update_chart_params = function(select){
alert(select.value);
<% ChartType.single_indicator.each do |ct| %>
$('#chart_type_params_' + <%= ct.id %>).hide();
<% end %>
$('#chart_type_params_' + select.value).show();
}
*Also, if I change the fields_for to f.fields_for and add #indicator.chart_param_values.build to the indicators_controller new/create methods, I get duplicated fields on the form based on how many chart_param_value records were built.
So I'm trying to set a name attribute of organizations and create a new organization or choose from a previously existing organization from the same form.
I've tried to follow Ryan Bates' railscast on the topic here: http://railscasts.com/episodes/57-create-model-through-text-field
I have also tried numerous solutions from stack. However, I can't quite seem to get it to run (that and I have a validation that does not recognize the virtual attribute I'm using)
so my organizations model:
class Organization < ActiveRecord::Base
has_many :materials
has_many :users
has_and_belongs_to_many :causes
has_and_belongs_to_many :schools, :join_table => 'organizations_schools'
####The following line has been edited ####
attr_accessible :name, :unlogged_books_num, :id, :new_organization_name
attr_accessor :new_organization_name
before_validation :create_org_from_name
validates_presence_of :name
def self.assign_school_to_organization(org, school)
orgschool = OrganizationsSchool.create(:organization_id=> org.id, :school_id=> school[0])
end
def create_org_from_name
create_organization(:name=>new_organization_name) unless new_organization_name.blank?
end
end
I have also tried the create_org_from_name as the following:
def create_org_from_name
self.name = new_organization_name
end
And this does not change the name to the organization name before validating or saving the instance.
I have also tried to change the before_save to before_validation, and that has not worked
My controller for organization (I also tried to change this in create)
def create
respond_to do |format|
#organization = Organization.new(params[:organization])
#organization.name = #organization.new_organization_name unless #organization.new_organization_name.blank?
if #organization.save
#school = params[:school]
Organization.assign_school_to_organization(#organization, #school)
format.html { redirect_to #organization, notice: 'Organization was successfully created.' }
format.json { render json: #organization, status: :created, location: #organization }
else
format.html { render action: "new" }
format.json { render json: #organization.errors, status: :unprocessable_entity }
end
end
end
And finally, I have what my form is doing currently:
<%= form_for(#organization) do |f| %>
<% if #organization.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#organization.errors.count, "error") %> prohibited this organization from being saved:</h2>
<ul>
<% #organization.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% #schools = School.all %>
<% #organizations = Organization.all %>
<div class="field">
<%= f.label 'Organization Name' %><br />
<%= f.collection_select(:name, #organizations, :name, :name, :prompt=>"Existing Organization") %>
Or Create New
<%= f.text_field :new_organization_name %>
</div>
<div class="field">
<%= f.label :unlogged_books_num %><br />
<%= f.number_field :unlogged_books_num %>
</div>
<div class="field">
<%= f.label 'School' %><br />
<% school_id = nil %>
<%= collection_select(:school, school_id, #schools, :id, :name) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
==================================EDIT============================================
So currently, when I try to make an organization with something only written in the virtual text field, My log tells me the following:
Processing by OrganizationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"igoefz8Rwm/RHrHLTXQnG48ygTGLydZrzP4gEJOPbF0=", "organization"=> {"name"=>"", "new_organization_name"=>"Virtual Organization", "unlogged_books_num"=>""}, "school"=>["1"], "commit"=>"Create Organization"}
Rendered organizations/_form.html.erb (7.1ms)
Rendered organizations/new.html.erb within layouts/application (8.0ms)
Completed 200 OK in 17ms (Views: 12.2ms | ActiveRecord: 1.0ms)
================================EDIT 2============================================
So this is what I get from the rails console if I try to create a new organization running this command: Organization.create(:new_organization_name=>"Virtual Organization", :unlogged_books_num=>"3")
irb(main):001:0> Organization.create(:new_organization_name=>"Virtual Organization", :unlogged_books_num=>"3")
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Organization id: nil, name: nil, unlogged_books_num: 3, created_at: nil, updated_at: nil>
If the function of create_org_from_name is self.name = new_organization_name, then the result of the same command from the console is blank:
irb(main):002:1> Organization.create(:new_organization_name=>"Virtual Organization", :unlogged_books_num=>"3")
irb(main):003:1>
You need:
before_validation :create_org_from_name
and
def create_org_from_name
self.name = new_organization_name if not new_organization_name.blank?
end
You don't want to do a create in your before_validation method.
transaction.rb model:
class Transaction < ActiveRecord::Base
attr_accessible :customer, :tickets_attributes
has_many :tickets
accepts_nested_attributes_for :tickets
end
ticket.rb model:
class Ticket < ActiveRecord::Base
attr_accessible :booking_id, :quantity, :transaction_id
belongs_to :transaction
belongs_to :booking
end
in the view page i have a nested rails form for multiple entries of ticket:
<%= form_for(#transaction) do |f| %>
<%= f.text_field :customer %>
<% #sezzion.bookings.each do |booking| %>
<%= booking.bookingdate %>:
<%= f.fields_for :ticket do |t| %>
<%= t.text_field :quantity, :value => 0, :class => "quantity" %>
<%= t.hidden_field :booking_id, :value => booking.id %>
<% end %>
<% end %>
<%= f.submit "create transaction" %>
<% end %>
When I'm submitting the form, I have the following error:
ActiveModel::MassAssignmentSecurity::Error in TransactionsController#create
Can't mass-assign protected attributes: ticket
I have attr_accessible :tickets_attributes and accepts_nested_attributes_for :tickets in the transaction model and there's still an error. Also when I add plural to the ticket on line <%= f.fields_for :ticket do |t| %>, the quantity field doesn't display.
Your form f is based off of a Transaction object, which has_many :tickets. I believe you should be using the plural :tickets rather than the singular :ticket in your fields_for.
<%= f.fields_for :tickets do |t| %>
If you always want a new ticket, you may need to do:
<%= f.fields_for :tickets, Ticket.new do |t| %>
to ensure that a create form shows up.
total re-edit -- sorry its been a while I had to refresh my memory
transaction.rb tickets_attributes is ok.
class Transaction < ActiveRecord::Base
attr_accessible :customer, :tickets_attributes
has_many :tickets
accepts_nested_attributes_for :tickets
end
transaction_controller.rb you must build the tickets.
def new
#transaction = Transaction.new
#transaction.tickets.build
end
new.rb or in your form, fields_for must be for :tickets as rob as pointed out:
<%= form_for(#transaction) do |f| %>
...
<%= f.fields_for :tickets do |t| %>
...
I think you might be missing the build part in the controller. hope that helps!
I have been working on this for a couple of days, searching here and there got me somehow close to my objective.
I am trying to have multiple images upload in my application.
Movie.rb
class Movie < ActiveRecord::Base
attr_accessible :title, :notes, :link, :screenshots_attributes
has_many :comments, :as => :commentable
has_many :screenshots
accepts_nested_attributes_for :screenshots, allow_destroy: true
belongs_to :cast
end
Screentshot.rb
class Screenshot < ActiveRecord::Base
attr_accessible :image, :movie_id
belongs_to :movie
mount_uploader :image, ImageUploader
end
Movie_controller.rb
def create
#movie = Movie.new(params[:movie])
#cast = Cast.find(params[:cast_id])
if #release.save
redirect_to cast_movie_path(params[:cast_id], #movie), notice: "#{t 'movie.created'}"
else
render action: "new"
end
end
_form.html.rb (movie)
<%= form_for [#cast, #movie], :id => 'fileupload' do |f| %>
<%= f.text_field :movie %></div><%= t 'movie.title' %><br />
<%= f.fields_for :screenshots do |s| %>
<%= s.file_field :image, multiple: true %><%= t 'movie.screenshots' %><br />
<% end %>
<%= f.text_area :notes %></div><%= t 'movie.notes' %></div><br />
<div class="actions">
<%= f.submit "#{ t 'general.submit'}", :class => "btn"%>
</div>
<% end %>
when I submit the form, only ONE image gets uploaded although when I "raise params.to_yaml" (for debugging purposes), I get NoMethodError
undefined method `name' for nil:NilClass
and Request parameters http://pastebin.com/4iZvtnED
I have three models, and i am trying to save onto a third table using simple form gem and checkboxes.
class Work < ActiveRecord::Base
has_many :skills, through: :skillships
end
The second model is
class Skill < ActiveRecord::Base
has_many :works, through: :skillships
end
The third is
class Skillship < ActiveRecord::Base
belongs_to :work
belongs_to :skill
end
Using the Work model i am trying to save the data on the skillship table. Something similar to this http://railscasts.com/episodes/17-habtm-checkboxes-revised. Can you please help.
EDIT
The form
<%= simple_form_for(#work) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, :label => 'Project Title' %>
<%= f.input :excerpt, :as => :text %>
<fieldset>
<legend>Skills Used </legend>
Would like to check the skills i used here.
</fieldset>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I tried..
<%= hidden_field_tag "work[skill_ids][]", nil %>
<% Skill.all.each do |skill| %>
<%= check_box_tag "work[skill_ids][]", skill.id, #work.skill_ids.include?(skill.id), id: dom_id(skill) %>
<%= label_tag dom_id(skill), skill.title %><br>
<% end %>
The reason i'm doing this it because work can have many skills used.
I was going about this the wrong way. A join table solved the problem.
rails g migration create_skills_works_table
Then
class CreateSkillsWorksTable < ActiveRecord::Migration
def self.up
create_table :skills_works, :id => false do |t|
t.references :skill
t.references :work
end
add_index :skills_works, [:skill_id, :work_id]
add_index :skills_works, [:work_id, :skill_id]
end
def self.down
drop_table :skills_works
end
end
Using simple form on the work view.
<fieldset>
<legend>Skills Used </legend>
<%= f.association :skills %>
</fieldset>