I'm creating user permissions for my project, so I created a model called "profile", each "profile" record will represent a module of my proyect, so a "user" has many "profiles", I need to create a form In which I can create multiple profiles to the user and modify all those records in a single form. The form would be like this , but I would like to know how I could do to create multiple nested records in a single form and that each one is independent since each record will have fields like the name of the module that identify it, for example "products"
So far I have something like this, but it only shows me a single field:
<div class="form-group">
<%= f.fields_for :profiles do |profile| %>
<%= perfil.hidden_field :Module, value: "Products" %>
<%= f.label :Bajas,"Bajas:", class: "control-label" %>
<%= f.check_box :Bajas %>
<%end%>
</div>
You should use form-group of div inside #user and change the div like this:
<%= form_for #user do |user| %>
<div class="form-group">
<%= f.fields_for :permission, #user.permission do |profile| %>
<%= perfil.hidden_field :Module, value: "Products" %>
<%= f.label :Bajas,"Bajas:", class: "control-label" %>
<%= f.check_box :Bajas %>
<%end%>
</div>
<% end %>
Related
I have a bare bones rails form. I have minimal javascript for adding and removing the category fields. I don't want it to be fancy so a rails specific solution is what I'm looking for.
The form is for Posts with a has_and_belongs_to_many relationship to Categories.
<%= form.collection_check_boxes :category_ids, Category.all, :id, :name, {prompt: "None", include_hidden: false}, {multiple: true} %>
<% #posts.categories.each do |category| %>
<%= form.fields_for :categories, category do |c_subform| %>
<div>
<%= f.hidden_field :_destroy %>
</div>
<div class="row clearfix">
<%= f.label :name, "Category name", class: 'form-label' %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<% if #posts.new_record? %>
<div>
<%= link_to "Remove Category", '#', class: "remove_fields", style: "color: red" %>
<%= link_to_add_fields "Add Category", form, :categories %>
</div>
<% end %>
<% end %>
<% end %>
I added the ability to select multiple categories and there is another fieldset below allowing the user to create new categories. The issue is while editing a post, if I deselect one of the previously selected categories (in this case, the one with ID of 3), on save I get the error: ActiveRecord::RecordNotFound Couldn't find Category with ID=3 for Post with ID=23.
I would love for the update action to just delete the posts_categories record connecting Post 23 and Category 3 without deleting Category 3.
I want to add that the form worked fine until I decided to add the multi-select functionality by making it checkboxes that could be deselected and also required the category names be unique.
I have a has_many through association for three models. One model has individual tests that can be administered. The second model is the name of a group of those tests. (The group name of triathlon will have associations to multiple records in the individual test model).
I want the user to be able to select a group name from a Dropdown
and when they do it passes a param to a specific action in a controller that then is tested through the association model.
I can get the form to look correctly, but not pass parameters to a specific action and then return to the same page.
I have searched and tried for hours to no avail. What’s the correct Cr form_for syntax (or something entirely different.)
Below is my current code
<% form_for #labwork, :url => {:action => "groupcreate"} do |f| %>
<%= f.form_group :lab_id, class: 'row' do |f| %>
<%= f.label "Group", class: 'control-label col-md-2' %>
<div class='col-12 col-md-10'>
<%= f.collection_select :lab_id, Labgroup.all, :id, :name, {prompt: "Choose a lab group "}, class: "form-control" %>
<%= f.error_messages %>
</div>
<% end %>
<div class="d-flex">
<div class="ml-auto">
<%= f.submit class: 'btn btn-primary' %>
<%= link_to "Cancel", labworks_path, class: 'btn btn-outline-default' %>
</div>
I also have a def creategroup in my labworks controller.
I'm building a rails app that has a an object that is created /nested underneath two objects.
Routes.rb
resources :pages do
resources :referralpages do
resources :rewards do
end
end
end
I've just added the rewards resource and have this for my form for creating a new reward
<%= form_for ([#referralpage, #reward]) do |f| %>
<% if #reward.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reward.errors.count, "error") %> prohibited this reward from being saved:</h2>
<ul>
<% #reward.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :level %><br>
<%= f.text_field :level, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :dscount %><br>
<%= f.text_field :discount, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
I need help getting the form_for ([#referralpage, #reward]) portion working.
Here's the error message I'm getting when clicking the new reward button:
undefined method `referralpage_rewards_path'
<%= form_for ([#referralpage, #reward]) do |f| %>
my guess is that it's routing to the incorrect path. What's the proper syntax to get this to work?
I think it should render this path
new_page_referralpage_reward
The point of this feature is to render rewards in the referral/show.html.erb page
I have created an index partial in my rewards view file and am rendering it in the show action of the referralpages/show.html.erb file.
I think you cannot put more than one resource path in form_for which cause invalid path.
Why you want to put 2 resource path in form?
Do you plan to save the same data for referral & rewards Model?
If, yes use just one path and make a create method in your controller to save to other model.
From this point of view:
The point of this feature is to render rewards in the
referral/show.html.erb page
If you only plan to render the data of rewards to referral/show.html.erb,
in your referral controller
def show
#rewards = Reward.all #example
end
Unless, you have model relationships like:
#Reward Model
belongs_to :refferal
#Referral Model
has_many :rewards or has_one :reward
With model realtionship:
def show
#referal = Referral.all #example
end
show.html.erb View # iterate it:
<%for referral in #referral%>
<% referral.rewards %> # need to iterate if has many
or
<%= referral.reward... %>
<%end%>
I want to implement dependent fields in my application. For that I'm using the dependent-fields-rails gem. I've set up everything with this tutorial. The good thing: It works. Kind of. In my brand model I have brands listet such as Adidas, Nike, ...
Now my two challenges:
I don't know what I have to fill in to the data-option-value=" " field because I want to show the select form for the category if a Brand is selected at all and not a specific one. If I put a value in the field (hardcoded) like data-option-value="Adidas" the category_select form only appears if I've chosen "Adidas" from the Brand dropdown.
I want to show only the matching categories for the Brand that was selected in step 1. My group model contains also the brand record so I can match a category to the brand.
To make it a bit more clear:
Brand model contains companies like Adidas, Nike, ...
Group model contains categories like Footwear, Shirts, ...
Here's what I got:
<div class="form-group">
<%= f.label :brand %>
<%= f.select(:brand, Brand.pluck(:company).uniq, {prompt:true}, {class: 'form-control'}) %>
</div>
<div class="form-group js-dependent-fields" data-select-id='warehouse_brand' data-option-value="Adidas">
<%= f.label :category %>
<%= f.collection_select(:category, Group.all, :brand, :name1, {prompt:true}, {class: 'form-control'}) %>
</div>
Any Ideas? Thanks a lot in advance!
I assume that a brand has many groups. If you don't want to use hard coded values then put the js-dependent-fields in a loop as shown in the code below.
<div class="form-group">
<%= f.label :brand %>
<%= f.select(:brand, Brand.pluck(:company).uniq, {prompt:true}, {class: 'form-control'}) %>
</div>
<% Brand.all.each do |b| %>
<div class="form-group js-dependent-fields" data-option-value= "#{b.company}" data-select-id="warehouse_brand">
<%= f.label :category %>
<%= f.collection_select(:category, Group.where(brand_id: "#{b.id}"), :brand, :name1, {prompt:true}, {class: 'form-control'}) %>
</div>
<% end %>
To show only the matching categories you can write a query that selects the related to the brand as shown in the code
Group.where(brand_id: "#{b.id}")
this actually works:
<div class="form-group">
<%= f.label :brand %>
<%= f.select(:brand, Brand.pluck(:company).uniq, {prompt:true}, {class: 'form-control'}) %>
</div>
<% Brand.all.each do |b| %>
<div class="form-group js-dependent-fields" data-option-value="<%= b.company %>" data-select-id="warehouse_brand">
<%= f.label :category %>
<%= f.collection_select(:category, Group.where(brand: b.company), :brand, :name1, {prompt:true}, {class: 'form-control'}) %>
</div>
<% end %>
First off, I'm pretty new to Ruby and Ruby on rails, so bear with me.
I'm attempting to have my categories section of my form utilize my category table in my db so that when the author wants to select on a category, they can just select the drop down menu and choose one. For now, I've hard coded them and this works
<%= form_for(#article) do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :author_id %><br>
<%= f.text_field :author_id %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.select (:title), [['Finance'], ['Marketing'], ['Programming'], ['Leisure'], ['Management'], ['Food']] %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
but, by switching the categories section to
<div class="field">
<%= f.label :category_id %><br>
<%= f.select :category_id, Category.all %>
</div>
it will give me a drop down box containing each hash, and I'm not to sure why, but I can't do Category.all.name to get the names (I'm assuming it is because it's in a hash). Any help would be great appreciated.
It looks like that you need to change you select use options_for_select tag:
<%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ] }), :include_blank => true %>
if you want to pre-populate category_id
<%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ]}, #article.category_id), :include_blank => true %>
For more information options_for_select
Have you defined the Category model in your Controller? The form can/will only know what the controller knows. It cannot know any more. If this form is attached to the articles_controller then it will have access to #article, but it won't have access to #category.
So this leaves you with a couple options. Define category somewhere in your controller, OR alternatively (and this would be my suggestion) setup some type of association for your Articles model.
Does Article have many Categories? This is a great opportunity to setup that has_many association, and then you can access the category model via Article's associations methods.
This is my suspicion, but we really don't have enough information here. Can you post your controller, and your article model so we can see everything that is going on? Also, what error do you get specifically?