ruby-on-rails,ruby,boolean,nested-attributes,sidebar
You can update your Result as follows: class Result < ActiveRecord::Base # rest of the code scope :good, -> { where(good: true) } scope :good_count, -> { good.count } end Let's perform some tests in rails console: u = User.create({ user_attributes }) u.results.create(good: true) u.results.create(good: false) u.results.create(good: true) u.results.count #...
ruby-on-rails,ruby,nested-attributes
In your create action, instead of this: @event.event_categories.build Try this: @event.event_categories = EventCategory.new do |ec| ec.event = @event ec.category = the_cattegory_you_want_to_specify # You need both of these as you are validating the presence of event AND category end ...
ruby-on-rails,nested-attributes
Make sure you have student_profile_id attribute/column present in educations table. After that, as you have mentioned, you need to build educations object on student_profile as: def new @profile = current_user.build_student_profile @profile.educations.build end ...
ruby-on-rails,ruby,user,nested-attributes
This will hopefully guide you in the right direction This is odd to me def index @averaged_quantifieds = current_user.quantifieds.averaged @instance_quantifieds = current_user.quantifieds.instance @averaged_quantifieds = Result.all.order("date_value") @instance_quantifieds = Result.all.order("date_value") end You define both of these twice and essentially overwrite each other? If your attributes aren't showing, it doesn't have anything to...
ruby-on-rails,nested-attributes
I figured it out thanks to advice from @davidwessman. What he told me was to display the html and inspect. I did and compared to the html from the edit action (the one that works). It then became clear that I needed to use the check_box_tag command instead of check_box....
ruby-on-rails,nested-attributes,params,rails-routing,nested-resources
With some help I derived this answer from a bit of kobaltz answer and some other help. The alternate method would be putting the information in hidden fields and passing it to the controller <%= @event.event_questions.each do |q| %> <%= q.question %><br> <% q.event_answers.each do |a| %> <%= a.answer %>...
ruby-on-rails,ruby,nested-attributes
The undefined method on NilClass is because your instance variable @survey is nil, and when you're calling Question.where(:survey_id => @survey.id).all you're getting that error. If you're associations are set up right you should be able to run @survey.questions and not have to perform the search on Questions. That's part of...
ruby-on-rails,model,nested-attributes
You can simplify your function as follows: @goal.status = @goals.tasks.map(&:complete).all? Assuming you want to know as soon as possible whether or not your goal is complete, you'll want to put this function in your Task model. You can do it as follows: def Task < ActiveRecord::Base belongs_to :goal after_save :goal_completed...
html,ruby-on-rails,ruby-on-rails-4,twitter-bootstrap-3,nested-attributes
You can try with includes if you have explicitly set has_many association in your Product model (assuming is is named product_images): app/controller/home_controller.rb: @products = Product.includes(:product_images) .where(product_images: { default_image: true ) .last(5) app/views/home/index.html.erb: <div class="col-md-3 col-sm-6 hero-feature"> <div class="thumbnail"> <% @products.each do |pd| %> <% pd.product_images.each do |i| %> <%= image_tag...
ruby-on-rails-4,rspec,nested-attributes
I think the routing error was indeed due to specifying followable_id rather than article_id as implied by the comment you received. The subsequent no method error, however, was due to making that change in the factory call as well. The following is what you should try: describe '#DELETE destroy' do...
ruby-on-rails,ruby,nested-attributes
You have a typo in your Comment model. Change 'DESC1' to 'DESC'
ruby-on-rails,nested-attributes,belongs-to
As I wrote in the comments, there's two ways of doing this. The first way is to add a hidden field in your subform to set the current user: = simple_nested_form_for(@issue) do |f| = f.input :title = f.fields_for(:comments) do |cf| = cf.input(:content) = cf.hidden(:user, current_user) = f.submit If you do...
ruby,ruby-on-rails-4,nested-attributes,strong-parameters
The problem is this line order_row_attributes.It should be order_rows_attributes. And with the date not being permitted,try changing the date attribute to some name like order_date. This should work private def order_params params.require(:order).permit(:customer_id, :order_date, :total, :order_rows_attributes => [:description, :quantity, :price, :order_id]) end ...
image,ruby-on-rails-4,twitter-bootstrap-3,nested-attributes,home
You can try this: app/controller/home_controller.rb class HomeController < ApplicationController def index @products = Product.last(5) @product_ids = @products.collect(:id) @product_images = ProductImage.where(:id => @product_ids).last(5) end end app/views/home/index.html.erb <% @products.each do |pd| %> <div><%= pd.product_name %> <% end %> <% @product_images.each do |pd| %> <%= image_tag (pd.product_image(:medium)) %> <% end %> ...
ruby-on-rails-4,simple-form,nested-attributes,wicked-gem
When you're using accepts_nested_attributes_for you must specify your attributes to whitelist them: educations_attributes and works_attributes (as shown in the API) def user_params params.require(:user).permit( :first_name, :last_name, :email, :password, :password_confirmation, :county_id, :area_id, :client, educations_attributes: [:school_name, :degree, :year_started, :year_finished], works_attributes: [:company_name, :work_title, :date_started, :date_finished] ) end ...
ruby-on-rails,ruby,forms,nested-forms,nested-attributes
I think that in order for your patient form fields to show up, you need to say @user.build_patient in your new action in your UsersController. This initializes a patient that is associated with the User instance you created. In case it helps anyone else, if the relationship between patient and...
devise,nested-forms,nested-attributes
After some digging and experimentation here is how I resolved this: Models class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :roles, :dependent => :destroy, :inverse_of => :user has_many :companies, :through => :roles accepts_nested_attributes_for :roles, :limit => 1, :allow_destroy => true end class Role < ActiveRecord::Base belongs_to...
ruby-on-rails,dynamic,nested-attributes
It's tricky because the data is column-wise but we need it row-wise. I'm not 100% sure I understand how the quantified data is laid out but the gist of the solution is, you need two separate loops since the heading <thead> and data <tbody> are in separate sections of a...
ruby-on-rails,nested-attributes,strong-parameters
use the singular profile_attributes if it's a has_one
ruby-on-rails,enums,nested-attributes,ruby-on-rails-4.1
Silly me. I can pass a second argument to the simple_fields_for which will limit the results. f.simple_fields_for :tiles, f.object.tiles.where(position: Tile.positions[:middle_column]) do |builder|...
ruby-on-rails,nested-attributes
It looks like you have the modify the model as such: accepts_nested_attributes_for :user, allow_destroy: true, update_only: true Adding the update_only: true prevents it from creating a new instance of user...
ruby-on-rails,ruby-on-rails-4,nested,nested-forms,nested-attributes
Change this line <%= w.link_to_remove "Remove attachment", class: "text-right"%> to <%= h.link_to_remove "Remove attachment", class: "text-right"%> ...
ruby-on-rails,forms,rails-activerecord,nested-attributes,strong-parameters
The problem, I would say, is that you are using a text_field input for category and sub_category. In order for this to work, you should use a select field and then provide another way in the UI to create categories and sub_categories. Replacing f.text_field :subcategory with (and removing the :category...
ruby-on-rails,controller,nested-attributes,fields-for
Give this a try- @cliente.build_local() instead of - @cliente.build_local ...
ruby-on-rails,ruby-on-rails-4,nested-forms,nested-attributes,strong-parameters
The usual way to do the above would be Controller def new @incorporation = Incorporation.new @company = @incorporation.build_company and in your view <%= form_for @incorporation do |f| %> <div class="panel-body"> <%= f.text_field :title, input_html: { class: 'form-control' } %> <h3>TEST</h3> <%= f.fields_for :company do |company| %> <%= company.text_field :name, input_html:...
ruby-on-rails,ruby,nested-attributes
AS per your model definitions, ProviderCategory has attributes category_id and provider_id. You are getting error as: undefined method `category_id' for #<Category:0x0000010184ff60> because instead of passing instance of ProviderCategory, you have passed an instance of Category class. Notice category being passed in =f.fields_for :provider_category, category do |pl| and as there is...
ruby-on-rails,associations,nested-attributes,has-many-through
Your join model name is not per Rails Naming convention. It should be called DeedTitleAbstract insted of DeedsTitleAbstracts. So fixing this class name including the database table name is probably the best thing to do instead of incorporating a workaround(shown below). The workaround for this is to supply the class_name...
ruby-on-rails,nested-attributes,railscasts,ruby-on-rails-4.2
I wound up solving this with jQuery. Not super elegant but very effective. Basically, I just added a click handler for the + icons which built the new row and inserted it where needed (basically just hacked out the jQuery necessary to replicate the HTML produced by Rails). In case...
ruby-on-rails,ruby-on-rails-4,carrierwave,image-uploading,nested-attributes
You are trying to iterate through an array of images but your params only have one image so the fix is replace: params[:images][:image].each do |i| @image = @service.images.create!(:image => i, :imageable_id => @service.id, :image_type => params[i][:image_type]) end with @image = @service.images.create!(:image => params[:images][:image], :imageable_id => @service.id, :image_type => params[:images][:image_type]) ...
ruby-on-rails,nested-attributes
The easiest would be to just modify the hash, like this: params["pages_attributes"] = params.delete("pages") You could put that in a before_action to ensure it happens for all the actions in the specific controller....
ruby-on-rails,ruby,nested-attributes,cocoon-gem
In your view you iterate with: @quantifieds.results.each do |result| In your index action you got @quantified = Result.order("date_value").all I guess it should be @quantifieds = Result.order("date_value").all ...
ruby-on-rails,forms,checkbox,nested-attributes,strong-parameters
I haven't been able to test this code myself, but I have implemented similar code, so the ideas should be correct. The trick here is using each_with_index, and then passing that index to your fields_for call. This way the each additional milestone_id that you add via a checkbox will be...
ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,nested-attributes
The association you created along with the accepts_nested_attribute_for method will create a event_file_attachments_attributes on which you can add the corresponding event file attachment attributes, here is a quick example: In the controller: @event_file = EventFile.new(:event_member_ids => data['event_member_ids'], :user_id => current_user.id, :company_id => @current_company.id, :event_id => data['event_id'], :status => 0, :event_file_attachments_attributes...
checkbox,ruby-on-rails-4,nested-attributes,strong-parameters,fields-for
Update venue_params method as below: def venue_params params.require(:venue).permit( :name,:address,:city,:state,:zip, parkings_attributes: [:id, :venue_id, :none, :street_free]) end Notice parkings_attributes(plural parkings) and not parking_attributes(singular parking). As you have 1-M relationship between Venue and Parking model you would receive parkings_attributes(plural parkings) in params hash BUT in your current code for venue_params you whitelisted parking_attributes(singular...
inheritance,ruby-on-rails-4,mongoid,nested-attributes,strong-parameters
as a workaround in the mongoid gem (version 4.0.0) in the file lib/mongoid/relations/builders/nested_attributes/many.rb I changed the creation of the object in the method process_attributes at line 109 to be klass = attrs[:_type].try(:constantize) || metadata.klass existing.push(Factory.build(klass, attrs)) unless destroyable?(attrs) instead of existing.push(Factory.build(metadata.klass, attrs)) unless destroyable?(attrs) and this solved my problem....
javascript,ruby-on-rails,nested-attributes
@AlexAtNet thanks for taking the time to answer! What i did was to iterate with a for loop through the desired ids. I added also a hidden field in my form to count the prices so the ids I needed for the for loop. My js is: jQuery(function(){ $("item_item_prices_attributes_0_regular_price.bind("change", function(){...
ruby-on-rails,ruby,ruby-on-rails-4,nested-attributes
If you want to update an existing data record, you have to include the object ID on the attributes_param: user = User.find(1) user_data_id = user.data.id user.update(data_attributes: { id: user_data_id, race: '2' }) ...
ruby-on-rails,devise,rails-activerecord,nested-forms,nested-attributes
"custom-err-msg"Custom error Gem does not really depend on rails version. So it will best to use or otherwise you have override full_messages method by yourself or you have add nested model validation in parent model itself.
So your syntax for get_object_or_404 is wrong. You don't pass it an object: it gets the object for you. So: user = get_object_or_404(User, email=email) Now you've got a User instance, and you want to get the relevant profile, so you can just do: profile = user.userprofile Alternatively it might be...
ruby-on-rails-4,parameters,nested-forms,nested-attributes
subcategories_attributes are sent correctly within your params hash BUT content attribute is missing in them. "subcategories_attributes"=>{"1398706662184"=>{"name"=>"TS1", "_destroy"=>"false"}, "1398706664804"=>{"name"=>"TS2", "_destroy"=>"false"}} Notice there is no content key passed. So, all the subcategories records passed in subcategories_attributes are REJECTED due to the condition you specified in Category model: accepts_nested_attributes_for :subcategories, :reject_if => lambda...
ruby-on-rails,ruby,ruby-on-rails-4,nested-forms,nested-attributes
You should learn the concept of form objects. You can easily implement this in Rails using ActiveModel. class SignupForm include ActiveModel::Model # define your signup form attributes attr_accessor :organization_name, :organization_note, :user_name, :user_fullname, :user_email def save organization = Organization.create( organization_name: organization_name, note: organization_note ) user = organisation.users.create(email: user_email, name: user_name, full_name:...
ruby-on-rails,file-upload,nested-forms,nested-attributes,form-helpers
I assumed that the form had multipart set, because specifying multipart => true for a form_for helper is not necessary according to Rails documentation. The fix turned out to be pretty simple, however - it works after setting multipart: true in a html options hash for the form tag: =...
ruby-on-rails,ruby-on-rails-4,nested-attributes
You don't use strong parameters in your create method so none of the parameters are passed over to your model. The model doesn't have any validation for the presence of attributes so it will just create you a blank record. In your controller, you need to change to use the...
ruby-on-rails,forms,ruby-on-rails-4,nested-attributes,fields-for
I believe I see the issue. In your new_project action, try this: def new_project @title = params[:ti] @project = Project.new @project.participants.build end To elaborate: fields_for is not going to render anything if the association is blank/empty. You need to have at least one participant returned by @project.participants in order to...
ruby-on-rails,join,nested-attributes
You can actually achieve this by setting up has_many through relationships between your models. There are great docs on this topic. class DataSet has_many :browse_options has_many :browse_option_datas, :through => :browse_options has_many :tradesmen, :through => :browse_option_datas end class BrowseOption belongs_to :data_set has_many :browse_option_datas end class BrowseOptionData belongs_to :browse_options belongs_to :tradesman end...
ruby-on-rails,devise,nested-forms,nested-attributes
I would overwrite the create action so that you can hardcode (and the user can't mess with) the role attributes. You're assigning them in the new action, but you'd need to have hidden fields so they get passed to create and persist into the database. However, that's not a good...
ruby-on-rails,scope,nested-attributes,validates-uniqueness-of
A better option in terms of performances is described below. It is tested and works just fine. Why? Mapping emails can consume a lot of ressources when a lot of emails are at stake, so its better to perform the scope directly with the database. How? Cashing the account_id in...
ruby-on-rails,ruby,ruby-on-rails-4,nested-attributes,collection-select
I prefer to do it more convenience way. # in your form <%= form_for(@post) do |f| %> ## your other fields <%= f.collection_select(:tag_ids, Tag.all, :id, :name, {include_hidden: false}, {multiple: true} ) %><br /> <% end %> #in your controller def post_params params.require(:post).permit([:title, :tag_ids => []]) end now instead of two...
ruby-on-rails,nested-attributes,has-one
I think you need to use polymorphic association as you need on model to belong to more than one model. You can use it like this: Edit: Changing the belongs_to :address to belongs_to :locatable as @vee corrected it. class Location < ActiveRecord::Base belongs_to :locatable, polymorphic: true end class Hotel <...
ruby-on-rails,ruby-on-rails-4,nested-forms,nested-attributes,has-many-through
In your new action: def new @food = Food.new @food.subs.build end and in your view: <%= f.fields_for :subs do |sub| %> When you're passing directly an object, this object becomes the new form_builder's object - rails have no idea it is in any way connected with original object so it...
ruby-on-rails,forms,ruby-on-rails-4,nested-attributes,link-to
Add the following in your form: <%= f.hidden_field :session_id, :value => params[:session_id] %> So that the session_id persists till the create action....
ruby-on-rails,many-to-many,nested-attributes
Update setup_new_project method as below: def setup_new_project(project) project.assigned_projects.build ## Updated this line project end Use project.assigned_projects(Notice plural) instead of project.assigned_project(Notice singular WRONG). User and AssignedProject model are in a 1-M relationship. So, you get dynamic method assigned_projects=(Notice plural), you are getting error as you called assigned_project=(Notice singular) which does not...
ruby-on-rails,ruby-on-rails-4,nested-attributes
If you send the parameter _destroy: 1 through your hidden field <%= f.hidden_field :destroy %> you instruct Rails to destroy the child moz object, or in your case, prevent it from being created. As for the second part of your question, if you inline the partial from this <%= f.fields_for...
ruby-on-rails,ruby,ruby-on-rails-4,nested-attributes
This is how you specify the order: has_many :experiences, ->{ order(:experience_end_date) }, dependent: :destroy That will sort by ascending; for descending do this: has_many :experiences, ->{ order("experience_end_date DESC") }, dependent: :destroy You can usually find answers to such questions in the documentation, either in explanations or in the example code...
ruby-on-rails,redirect,ruby-on-rails-4,nested-attributes
Are you defining routes twice because that can cause issue in rails. You should only define them only once. resources :models do resources :lineitems end Also run rake routes from the console and see what routes are forming. Actually when you are using this @model.lineitems.build you are re-assigning the lineitems...
ruby-on-rails,nested-attributes,has-many-through,collection-select
First you need to understand more about controller actions. You can read about them in the Rails Guides For example: def create @match = Match.create(match_params) @home_opponents = @match.home_opponents.create!(params[:match_opponents]) @away_opponents = @match.away_opponents.create!(params[:match_opponents]) if @match.save redirect_to @match, notice: 'Match was successfully created.' end end Match.create already saves the object, which you save...
ruby-on-rails,ruby-on-rails-4,activerecord,rspec,nested-attributes
The issue is in sign_up_spec.rb. Your test has a let for user, which means the first time you mention user in your tests it will create a user. However, your application code is supposed to create the user itself. As I said in the comment, this is why in your...
ruby-on-rails,ruby-on-rails-4,nested-attributes
The clue is in the error: Goals contact can't be blank In your Goal model, you have the following validation: validates_presence_of :title, :due_date, :contact_id Try removing contact_id to test if it will accept or not. If it does accept, it means you're not passing the contact_id through your params to...
ruby-on-rails,activeadmin,nested-attributes,formtastic
You forgot to pass your form builder to the fields_for block. <%= f.inputs "Track Exclusivity", class:'inputs align-left' do %> <%= f.input :exclusive %> <%= f.input :full_exclusivity, label: "Fully Exclusive" %> <%= f.fields_for :exclusivities do |ff| %> <%= ff.input :notes %> <%= ff.input :staff_notes %> <%= ff.input :industry %> <%= ff.input...
ruby-on-rails,activeadmin,nested-forms,nested-attributes
Turns out I was defining ":browse_option_data" wrong and I should have been calling for ":browse_option_datas" and also defined it this way in the Model for has_many.
ruby-on-rails,devise,nested-forms,nested-attributes
You don't really need to use nested attributes in this case at all. There is no reason to pass the user_id and app_id via the form either since they are known in the controller. Doing so just opens up the door for potential mischief. Like sending user_id: 1, body: 'My...
forms,ruby-on-rails-3.2,nested,nested-forms,nested-attributes
Family and User are associated with 1-M relationship. In your view families/new.html.erb Change <%= f.fields_for @new_user do |ff| %> To <%= f.fields_for :users, @new_user do |ff| %> ...
ruby-on-rails,ruby,undefined,nested-attributes,cocoon
Answer to Your Specific Problem: In your index action you have: @quantified = Result.all.order("date_value") Calling .all before .order is the cause of your error. In Rails ActiveRecord, the .all command immediately triggers a fetch of items from the database. Since database IO fetching commands like .where and .order exist to...
ruby-on-rails,ruby,nested-attributes
You don't need to iterate before fields_for since it renders your form for the number of existing associations. You can confirm this by changing the number of @taxable_pays to 3 and see how you get 9 (and not 6) items in your form. Change your controller to the following: def...
ruby-on-rails-4,associations,nested-attributes,has-many
Update fields_for in your form as below: <%= q.fields_for :answers do |a| %> <%= a.text_field :content %> <% end %> Note pluralized :answers and not :answer. As Entry model is associated to Answer in a 1-M Relationship, you should be using plural :answers in fields for. Right now as you...
javascript,ruby-on-rails,ruby-on-rails-4,nested-attributes,link-to-function
That Railscast has several flaws - the most prominent being it only allows single form elements to be appended to the page (it preloads the JS link with a form element & keeps putting the same element onto the page -- problem being this element has the same id as...
ruby-on-rails,nested-attributes
I'll go out on a limb here and make assumption that you're working with a form submit and you don't want to add the record unless the name field is present. To give the name field a "required" characteristic it should be declared in the model itself, community.rb, for instance:...
ruby-on-rails,ruby,nested-attributes,unique-constraint,updating
This is happening because the widget updates do not happen all at once - they run one at a time. Rails checks uniqueness by querying the database for an existing row with the same data, and stops if it finds one with a different ID than the object you're trying...
ruby-on-rails,rails-activerecord,nested-attributes
First of all: you've set up an association (has_many :questions), so you can just use @quiz.questions instead of a manual search (Question.where...). You just need to specify a sort order and then fetch a single record based on a minimum-value. Let's say you choose the id (created_at or any custom...
ruby-on-rails,nested-attributes
Problem is solved by removing the validations for project_id and user_id in the join table "AssignedProject" So the join Model looks like that: # Join Model AssignedProject class AssignedProject < ActiveRecord::Base belongs_to :user#, class_name: "User" belongs_to :project#, class_name: "Project" attr_accessible :project_id, :user_id, :position, :project, :project_attributes accepts_nested_attributes_for :project validates :position, presence:...
ruby-on-rails,forms,ruby-on-rails-3,nested-attributes
Update: w/o nested forms So I hear you only want to only edit the vehicle_report without touching the vehicle. Since you already have your vehicle_report as nested resource, you can modify your controller as follows: class VehicleRecordController < ApplicationController ... def new @vehicle = Vehicle.find params[:vehicle_id] @vehicle_record = @vehicle.vehicle_records.build end...
ruby-on-rails,ruby-on-rails-4,nested-attributes
It is more likely to be an issue with validations than with strong parameters. In your case the user.email seems to be constrained to be unique, but it could be anything on the user or member models. Of course a forbidden parameter can also be a reason a validation fails...
ruby,hash,hashmap,nested-attributes,ruby-on-rails-2
You can use the form of Hash#merge that takes a block to produce the desired result in a compact manner: data1.merge(data2) { |_,ho,hn| ho.merge(hn) { |_,o,n| (o==n||o==''||n=='') ? nil : [o,n] } .delete_if { |_,v| v==nil } } .delete_if { |_,v| v.empty? } #=> {"3"=>{"passenger_type"=>["ADT", "ADTT"]}, # "1"=>{"passenger_type"=>["ADTT", "ADT"], "last"=>["JONES",...
ruby-on-rails,ruby-on-rails-4,nested-attributes
Ok, got it!! The problem is the one-to-many relationship and the way I tried to access a single instance of user. The correct way to do it is: <% current_account.users.each_with_index do |user, index|%> <%= f.simple_fields_for :users, user do |user_form| %> <%= user_form.file_field :avatar, :error => false %> <% end %>...