i have a rails form ive made in which the form fields are extracted from the database. i did it like this because for different products, there are different form fields. i could have made one big order form to do it, and if the product field didnt apply to the product it would be left blank, but it seemed like making the fields being called from a database made more sense because there are 30-40 fields per order. anyways the error in which im running into is when im extracting the row field_type, it prints out the literal value instead of putting it in rails. heres what it looks like:
<% #form_field.each do |field| %>
<p>
<%= "f.#{field.field_type}" %> #this prints out f.text_field
</p>
<% end %>
Instead of printing out f.text_field, i would like it to actually make a text field. I tried using raw but no look seeing as thats for html. is there a way to do this in rails?
You'd need to build up the string and send it to f, like f.send(field.field_type) (untested) along with any arguments needed for that particular form field type.
Related
I have to create a feature to manage my contracts. Everything was going fine working with the strings etc.. but I have to create checkboxes in my scaffold for an example
()Toll
()Chemist
And I want to save this if the boxes are checked.
I tried put this code into the Form file, it appears, but don't save the data when checked, I guess that is not the correct way, because there is nothing created in the database.
<input type="checkbox" name="tag_ids[]" value="1" />
I'm planning to generate all the checkboxes as strings when generating the scaffold and try something else.
Does anyone have a better idea how can I accomplish this, could be the easiest way as possible is not for real development. Thanks for all.
first you should check if the vars are whitelisted,
second you should try the rails helper for this:
<%= form.check_box "tag_ids[]", "Chemist", false, id:"1"%>
with this syntax the input will be stored in an array, so you can add multiple checkboxes for the same variable. the input will than be stored ["Chemist","Toll"]
Later you can split these easily by .split
You can add the following to your form
<% Tag.all.each do |tag| %>
<label>
<%= f.check_box_tag "contract[tag_ids][]", tag.id, #contract.tags.include?(tag) %>
<%= tag.name %>
</label>
<% end %>
This can then be passed through your parameters as long as your strong parameters allow them eg:
params.require(:contract).permit(.... , tag_ids: [])
see here for a proper discussion: https://kolosek.com/rails-join-table/
Also if you are interested I'd look at using simple_form (https://github.com/plataformatec/simple_form) for your rails forms. It makes all of this really easy.
I'd like to have a drop down in my Rails form where users can select an area of a city, e.g. "Marchmont", "New Town", "Baberton" etc, when adding an order. I'd like that once they have made a selection, this will then be the default selection for the following times they use the form to add an order (so that they don't have to keep selecting it) but also that they can change their selection at any time. Hope that makes sense. I'm using the Simple Form gem. Thanks in advance! :)
#Steve
I will make a couple of assumptions.
1.) you know how to create forms within the rails templating engine.
2.) you understand how to create a dropdown menu using the Simple Form gem
So you have a couple of options based on what you actually want to accomplish. Based on what you are briefly describing, it sounds like you have some kind of an e-commerce/checkout situation that you want auto-completion to make it easier for a user.
there are a couple of approaches to storing this data.
Saving the user Data.
1.) Save it right on the user model under district_of_last_order
2.) Save it right on the order model that a user has_many orders. Then you can pull the first order's city district and select that
Personally I would lean on #2 as you probably want to be able to tightly couple the order with the user and saving that information twice is redundant since you can always do something like current_user.orders.first.district or whatever,
in your ERB where you build the form you can then do something along these lines:
<%= simple_form_for(#order) do |f| %>
... other input fields
<% if current_user.orders.first %>
<%= f.input as: :select selected: current_user.orders.first.district %>
<% else %>
<%= ... your regular dropdown menu here without the default %>
<% end %>
... rest of your form
If you have the option of using gems, I have had good results with select2-rails.
So, I have a nested form where Shelter has many Cats (just using cats as an example to melt the hearts of those eager to help).
So, in my nested form I can use:
<%= f.object.shelter_id %>
which gives a value when I run the application. But, and here is the cute cat's but:
<%= f.object.shelter.name %>
doesn't work, nor does:
<%= Shelter.find(f.object.shelter_id).name %>
Above statement gives an error can't find Shelter where 'id'=
One would think the value would be passed to the query as it is displayed when the app is run? How do I access the associated values in the form? (Its only in Edit, my Show and other controllers and views work fine.)
And yes, the associations are declared in my models.
I have a Rails model with 2 fields: 'name' and 'bucket'. Name is a string and bucket is a hash of the form: bucket: {red: 1, green: 2, ...}.
I created a rails form to populate these fields but while name works properly, the rest don't even appear as values.
This is the point that it doesn't work, when I am trying to bind a color to a value provided by the user, with default value 0.
<% #board.fetch_all_colors.each do |color| %>
<%= form.label color.to_sym, "#{color}:" %>
<div class="input">
<%= form.text_field :bucket, value: 0.0 %>
</div>
<% end %>
The thing is that when I debug and print params, I only find an empty bucket hash. One problem seems to be that because of the each loop, I only get the last input text.
Edit: I found a workaround that takes me half there. Instead of a text_field, I am using the following:
text_field_tag "bucket[]"
The above uses my input normally and puts it in an array. The problem with that is that since the input is dynamic (and could have labels come and go, I can't guarantee for the position of the array (e.g. that 3rd position is always 'red'). Can I use something like the above but with a hash?
While JSON-like objects are supported by rails DB drivers, the rest of the rails stack doesn't have seamless API's for specifying multi-value column attributes.
You'll need to dynamically build your hash in the controller from custom-defined attributes which don't have a 1:1 mapping to your model.
Alternatively in a more Rails-like fashion, you could use a has_many relation, storing your buckets as a separate model in the database which is joined to your Board model. accepts_nested_attributes_for would help you do this.
Background: My goal is for a view to display a list of "condition" has_many objects, which are themselves STI subclasses of a StateDescription. I want a user to be able to pick what type of state description they are from a drop down menu (which will conditionally display a different type of form, eventually)
Inside of my main forms, I am doing a nested form like this:
<%= f.fields_for :conditions do |e| %>
<li>
<%= e.select(:type, StateDescription.subclasses.collect{|x| x.to_s}, options_for_select(StateDescription.subclassSelectForms)) %>
<br>
<%= e.label :title %>
<%= e.text_field :title %>
</li>
<% end %>
This works just fine with the text field at the bottom there (I can change values and save them, etc). But when I try to do a select statement, it explodes.
Specifically, if I don't use e.select and just do:
<%= select(:type, StateDescription.subclasses.collect{|x| x.to_s}, options_for_select(StateDescription.subclassSelectForms)) %>
it renders just fine, but doesn't actually change any values as it is not associated with a model.
If I get rid of trying to have it display a value different than it submits and just do
<%= e.select(:type, StateDescription.subclasses.collect{|x| x.to_s}) %>
Then it works just fine(I can submit, the value is saved to the database, I can retrieve it, etc).
I can LEAVE it like this, but I had wanted to have my more human readable string display instead of a rails class thing (which might look like gibberish to a non-programmer end user).
So, my question is: Why do options_for_select break when nested and associated with a form, but dont' seem to when not associated with a form? Is it just a known bug? Am I doing something wrong?
Edit:
.subclasses is a standard rails calls which returns a list of subclasses for an object.
.subclassSelect forms goes through each subclass, and turns it into a hash of the form:
{subclass.to_s => subclass.human_readable_string} and compiles all of them into an array.
I know this array of hashes works, because if I do NOT put "e" on the front, it displays correctly and works "right" for what I have done (i.e. the subclass is returned correctly based on which human readable string I select from the drop down menu, but since it's not actually associated with any form, it doesn't get set in the controller).