Complex form logic (nested attributes, blongs_to) - ruby-on-rails

I have models:
advert.rb
belongs_to :car
belongs_to :user
car.rb
belongs_to :engine
belongs_to :door
belongs_to :bumper
engine.rb
belongs_to :brand
...and many more, as you understand
I need to make 1 form for creating advertisement that would include car section, where i can fill all data about car (engine model\brand, door model\brand). Don't ask my why it is so detailed).
So basically it means that i need to create advertisement -> car -> engine, door, bumper in one form. I triend to user accepts_nested_attributes_for in model, #advert.build_car in controller and fields_for in view, but it is not working (maybe my mistake). In view i get something like advert[car][engine][model] (not like with has_many association, where i get advert[photos_attributes] for example).
What is the best solution in my case to handle all in one form. And please, provide code example controller\view\model.
By the way, i already spent a day searching for the best possible way of solving this problem.

You can use th standard Rails form with nestings:
http://guides.rubyonrails.org/form_helpers.html#nested-forms
Also check Simple Form:
https://github.com/plataformatec/simple_form/wiki/Nested-Models

Related

Dynamic select and filter in Rails 4

Hi I'm a beginner to Rails and I've been struggling to create a filter feature, I've done some googling but I feel like I'm in a bottleneck situation right now, would really need some help.
Mock up:
I have an Event model, with title, description etc, and now I want to add Court, House and Event type into the model. When a user create an event, they must choose a court, a house and what type of event it is.
User must choose a court then select a house, as each house name is different in each court.
Preferably the house dropdown should only appear after court value is selected.
The most important part is the users can filter their search later on, and get a list of events based on the selections they've made.
/events?court=foo&house=bar&event_type=fun
And it's ok if the user only choose one choice and clicked Search
/events?court=foo # show all foo court events
/events?court=foo&event_type=fun # show all fun type events at foo court
My Questions
From some answers I've seen, some people have created seperate models for Court, House etc. Is that necessary in this case? I'd like to keep it all in Event.rb, so I can do
validates :court, :house, :event_type, presence: true
For the View, we would also need JavaScript right? Do we use it along side with a Event method like this?
def return_house(selected_court)
if selected_court == 'foo'
['bar1', 'bar2', 'bar3']
elsif selected_court == 'baz'
['qux1', 'qux2', 'qux3']
end
end
I've been using f.input for dropdown selections, but I've seen people use f.collection_select?
Because a court can be associated with multiple events, you'll need to create separate models and use a one_to_many relationship.
class Event < ActiveRecord::Base
has_one :court, through: :event_court
end
class EventCourt < ActiveRecord::Base
belongs_to :event
belongs_to :court
end
class Court < ActiveRecord::Base
has_many :event_courts
has_many :events, through: :event_court
end
You'll also want to do a similar thing for event_type.
This type of setup is why you see other people using collection_select.
I assume a court can also have multiple houses, so you'll want to create a has_many relationship between courts and houses
class Court < ActiveRecord::Base
has_many :houses
end
class House < ActiveRecord::Base
belongs_to :court
end
Note: This is not functional. Do not just copy paste it. Instead, read this: http://guides.rubyonrails.org/association_basics.html
I assume you are not just new to Rails, but also web dev. If that is the case, you might be in over your head. You are doing some fairly advanced Rails stuff.

Rails activeadmin new action with related models

In my rails app i use active admin, now i have such model (only part)
class Distributor < ActiveRecord::Base
attr_accessible :co****ime
has_many :price_lists
has_many :other_products_cross_lists
end
class OtherProductsCrossList < ActiveRecord::Base
attr_accessible :cross***pe_id
belongs_to :product_type
belongs_to :distributor
belongs_to :accumulator
belongs_to :oil
end
class Accumulator < ActiveRecord::Base
attr_accessible :capa***age
has_many :other_products_cross_lists
end
For security, i * attr...
So if i create a new Accumulator, i also need so, that with accumulator field's i see OtherProductsCrossList fields, and create accumulator, and cross for it, also i need to do such button :more, so if i need to create more than one cross for this accumulator it will do this. Also note, that id and refferertable_id must be same... How to do this? Form with another form for create action?
if you have a complicated/nested form, I don't think Active Admin is a good choice. You have to learn its DSL and have to face its bugs.
And writting your own implementation is:
not so hard with ERB/HAML's help, ERB/HAML is more easy to use than ActiveAdmin's View DSL.
very flexible, you are free to change the pages(CSS) and the models.
I tried ActiveAdmin for several projects, but very soon I abandoned it, because not only it gives me too few benefits/convenience, but also gives me too many difficulties to make my own customization.
To me, Active Admin is similar with the "Scaffold", but much worse.

Rails checkboxes for a has_many :through association

A person can compete in various events, but they must enter a partner's name for that event. This association is stored in an entry, which contains a field for the partner's name.
class Person < ActiveRecord::Base
has_many :entries
has_many :events, :through => :entries
validates_presence_of :name
end
class Event < ActiveRecord::Base
end
class Entry < ActiveRecord::Base
belongs_to :person
belongs_to :event
validates_presence_of :partner_name
end
The question is: How do you create a single page form that allows a person to enter themselves in multiple events and input their partners' names? I've tried to implement an all_entries method in the person model that will return an array of entry objects for all the available events, and an all_entries_attributes method that will update, create, and delete entry objects, but I can't seem to find a good, clean way to do this. I know this is a rather open ended question, but this must be a pattern that someone else in the rails community has encountered before, so I'm hoping there is a good solution to it.
If you are still looking for the answer you might want to check out my question and answer that I found.
So you want a page in which you can create events for a user, and add people to those events
I won't give you a plain solution because there's some work to an implementation for this, but I will recommend checking out these railscasts about nested model forms
part1 and part2

not sure how to model in ActiveRecord

I am making a simple event system (like physical events, not software events). It has the following structure. Each event will have a set of slots (think a music event, opening act, healiner etc...). Each event_slot will be a reference to a tag. Right now I have the following but I think this is not going to work:
class Event < ActiveRecord::Base
#id primary key
has_many :event_slots, :order => "sort desc"
has_many :tags, :through => :event_slots
end
# event_slots will be populuated with tag_id
class EventSlot < ActiveRecord::Base
# event_id, tag_id; will also have a sort value to sort these
belongs_to :event
belongs_to :tag
end
the issue is that event_slots will have a tag_id. In other words we'll be adding the tags and associating them in place (like physically in place in a web form).
class Tag < ActiveRecord::Base
has_many :event_slots
end
I am not sure if this modeling will work. Any ideas on how to implement / improve this this? The has_many :through seems not be done correctly.
thx
That should work fine. I would perhaps try use a better name than EventSlots because its not intuitive what it represents. If its aimed at the music market something like Acts would perhaps make more sense.
Using something like accepts_nested_attributes_for on the Event model can help eliminate the need for a controller for the second nested model (Act/EventSlot). Take a look at http://railscasts.com/episodes/196-nested-model-form-part-1 if you havent already.
Lastly I would consider not rolling your own tagging system. There are already plenty of well tested gems you can use to provide the functionality you need. Check out https://www.ruby-toolbox.com/categories/rails_tagging.html to find one that might suit your needs.

Help with rails content filtering

Im creating my own blog managing app in rails (for experimental purposes).... What would the best way to get this done?
I have posts and categories.
I want to have a dropdown of categories for the user to select one when they create a new post.
Now, each user will have different privileges so not all categories should appear for all users....
Right now Im at the point where I can create posts and choose which category I want... I havent added any filter per user support....
please help me on where should I go now??
First you will need to implement authentication and authorization. There are many good Rails tutorials on these subjects so I won't go into more detail here.
At this point you will have models for User, Post, and Category. You need a list per-user of authorized categories. A naive model:
class User < ActiveRecord::Base
has_and_belongs_to_many :categories
end
But that is misleading, because a user isn't actually "in" any categories. You probably want something like a join model like so:
class UserCategoryAuthorization < ActiveRecord::Base
belongs_to :user
belongs_to :category
// More fields here; possibly:
// belongs_to :authorized_by, :class_name => 'User'
end
class User < ActiveRecord::Base
has_many :user_category_authorizations
has_many :authorized_categories,
:through => :user_category_authorizations,
:source => :category
end
To start with I would give Users a has_many categories relationship(Which you could turn into its own model object at some point if this idea gets more complicated..or now if it already makes sense) and then assuming you already have log in functionality you can ask the logged in user for its categories and populate the drop down appropriately.
If this is a security issue rather than just convenience then you will need to validate the chosen category is in the users categories when the form is submitted back to the server.
If you don't already have logins I believe there are several rails plug-ins that attempt to help you get this functionality quickly.

Resources