I've been using simple_form for rails together with HAML
I have a number of line_items they have a unique id which i retrieve from the Order object
= f.simple_fields_for :line_items do |line_items|
.f.input :id unless #order.status?
.f.input :cost unless #order.status?
However, whenever i iterate through these list, i would like to hide (remove from the form) the line_items id, i try to put a unless statement to remove other parameters and it works (eg. :cost) but for the :id it appears to be a hidden fields
<input id="order_line_items_attributes_2_id" name="order[line_items_attributes][2][id]" type="hidden" value="26">
How can i successfully remove IDs from appearing in the fields just like how i did it for :cost?
try this:
= f.simple_fields_for :line_items, include_id: false do |line_items|
more on this: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
Rails thinks that you should NEVER need to edit the :id, so it makes it difficult or impossible. If you want to DISPLAY the :id, that's fine, but there would be limited reason to present the :id such that it can be edited.
I'd just always display the id in the form.
A work around...
I tricked the system, by rending the fields as i uses HTML comment to stop it from appearing and stopped it from being submitted
/ =f.input :id unless #order.status?
I uses = instead of - so as the fields would be able to be rendered however hiding inside the html comment
<!--<input class="hidden" id="order_line_items_attributes_2_product_id" name="order[line_items_attributes][2][product_id]" type="hidden" />-->
Related
I'm trying to get the following line to work in my Ruby on Rails project's erb.html file:
Student City:<input type="text" name="student[city]" value = <%= #student.city %>>
(This whole thing is inside of a form tag)
Everything works fine unless I give value a multi-word value, then it'll cut off everything after the space in the string.
To be specific, if student.city == "San Francisco", my page loads a text field with "San". I've tried changing it to different data types and manipulating the string and it still does the same thing. (Interestingly, trying to print the result after splitting the string into an array will display
["San",
Is this a known bug? Any ideas for a work around? If this is an actual bug with erb, how do I submit a bug report?
I wonder why you don't use Ruby on Rails' form helpers?
Something like:
<%= form_for #student do |f| %>
Student city: <%= f.text_field :city %>
# ...
<% end %>
Read more about form helpers in the Rails Guides.
You need to enclose them with double quotes:
Student City:<input type="text" name="student[city]" value="<%= #student.city %>" >
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).
I have a variable that I want to pass with a form in rails.
The variable is not convenient because it is not part of the model. I really don't want to create a column in the DB just so I can pass the variable.
When I try to pass the variable using something innocuous like:
<input type="hidden" id="blah" value=<%= "#{#blah}" %> />
The hidden field turns up correctly in the source, but doesn't pass with the params hash. When I code it properly with something like id="review_blah" rails assumes it's part of the review model and gets angry because it isn't part of the model.
Isn't there some workaround?? Some way to pass a variable easily for these circumstances, without rebuilding your model?
Thanks
You need to define an attribute_accessor for the virtual attribute and you need to add it to the attr_accessible list so it can be used in form. Like this:
attr_accessor :blah
This makes the blah and blah= methods available which you can use to deal with the incoming data.
def blah=(data)
#do sth. with the submited data here
end
In this case you could access the data entered in the "blah" field as the local variable "data".
Then you need to make it a part of the regular form:
<%= f.hidden_field :blah, :value => <Your value here> %>
So the last thing you need to do is adding the field to the attr_accessible list.
attr_accessible :blah,....
Otherwise the request would fail because of mass assignment protection.
In model
attr_accessible :blah
Though it's very ugly you can do something like following
<input name='x' type="hidden" id="blah" value=<%= "#{#blah}" %> />
in your controller
#model.save
Model.update_all({:my_attribute => params['x']}, {:id => #model.id})
You need to set the name of the input parameter I think. otherwise it will not be contained in Rails params hash.
<input type="hidden" id="blah" value=<%= "#{#blah}" %> name="blah"/>
class ModelName
attr_accessor :blah
end
#model = ModelName.new
#model.blah = "blah blah"
#model.blah #will return "blah blah"
You don't need to have a column in your table. Check here
This way you can have
=form_for #model do |f|
=f.hidden_field :blah
This will pass blah along the object in the params hash.
Problem solved. HTML5 localStorage messed with me.
I'm trying to populate a form with parameters from the new()-method, and I can't get it to work.
Every user has default values for the form saved in the database(in a table called defaults), and when you create a new record I want it to be populated with the values from that table.
#default = Default.find_by_user_id(current_user.id)
#invoice = Invoice.new(:title => #default.title, :company_information => #default.company_information)
render 'create'
and then in my view:
form_for #invoice, :url => { :action => "create"} do |f| ...
What happens is that the values that are default for invoice are created, but not the ones created in the new()-method.
The weirdest part is that when I check the source code after the page is loaded, the inputs value attributes is filled with the correct information, but not rendered on the page...
What you're doing here:
Invoice.new(:title => #default.title, :company_information => #default.company_information)
Makes sense and should work…unless those fields are protected from mass assignment.
class Invoice << ActiveRecord::Base
attr_accessible :some, :other, :fields
...
end
This would allow you to set :some, :other, (and) :fields when you initialize your Invoice object, but it will prevent you from setting any other "attributes".
Strange, I don't see anything wrong with what you are trying to do... maybe something on the browser side (javascript, css, etc) is fowling things up?
Check to see if there is something selectable inside the form inputs or try creating a vanilla form without any javascript or css. Or, you might even try simply printing the contents of the attribute in the html (without using input/textarea tags) using something like:
<%= #invoice.title %>
This will at least help confirm that the default values where indeed set. Additionally, using:
<%= f.object.title %> # place me inside the form_for block
will help you confirm that the form builder instance also has the correct value.
Good luck.
I've got a user model which contains a field called, lets say, text1. In the users/new form I want to have 4 individual text boxes for this text1. In my model I will take values from these boxes and concatenate them together as comma separated values and store them in the DB. To give you an understanding, this is what I want.
<input type="text" name="user[text1][]" />
<input type="text" name="user[text1][]" />
<input type="text" name="user[text1][]" />
<input type="text" name="user[text1][]" />
How do I get this using form_for helper method? For now please don't worry yourself about the accessor method in the model, that is all taken care of. Thanks a ton.
Add few virtual attributes to your User model
class User < ActiveRecord::Base
attr_accessor :text1_part1
attr_accessor :text1_part2
attr_accessor :text1_part3
attr_accessor :text1_part4
def before_validation
self.text1 = "#{self.text1_part1}#{self.text1_part2}#{self.text1_part3}#{self.text1_part4}"
end
# make sure you fill the correct values for
# the text parts for an existing record.
# This can be done by implementing `after_find` or overriding
# `text1=`.
end
In your view code use the new attributes instead of text1
<% form_for(:user) do |f| %>
#some code
<%= f.text_field :text1_part1>
<%= f.text_field :text1_part2>
<%= f.text_field :text1_part3>
<%= f.text_field :text1_part4>
#some code
<%= f.submit "Save">
<% end %>
The previous answer gives a solution, so I am just providing some background as to why what you are asking for does not work they way you might hope.
You can indeed create a form with multiple input fields of the same name, and that data will be posted. However, when rails receives the post it automatically parameterizes the post data and/or url parameters. It essentially splits on the & and assigns the key/value pairs to the params hash. The outcome of this is that params[:user][:text1] (from your example) will have the value of the last instance of the user[text1] it encountered, since it is simply a value assignment to an existing key. You might want to dig into ActiveRecord multiparameter assignments to get an idea of how datetime attributes work, since they are similar to your use-case.
I am working on something similar and it sounds like maybe serialization is what you are looking for. Unfortunately I don't have my issues solved yet, so I can't provide anything more concrete.