Rails for nested array hidden input field in form (Haml) - ruby-on-rails

What would be a better "Rails way" of doing it
- shops.map(&:id).each do |id|
<input id="p_shop_ids_#{id}" name="p[shop_ids][]" type="hidden" value="#{id}" />
I recently read that in Haml is in this sense downwards compatible. But it feels like it should be done with the rails checkbox helper instead

You can use the hidden field helper.

Based on #Sontya's comment (thanks!!)
- shops.map(&:id).each do |id|
= hidden_field_tag "p[shop_ids][]", id, id: "p_shop_ids_#{id}"
produces the correct output (I had to add the id-option)

Related

Is possible to add a namespace to the inputs in a form divided by partials?

I'm using simple form and I have a long form divided in partials, each partial represents a category.
For example, identification_form is:
I'd like the inputs to get a name like this:
Is possible to do this?
I can change the names by myself, but the problem is simple_form is not able to create the names for the stat_date and end_date (act[start_date(1i), act[start_date(2i), act[start_date(3i), it adds the same name for the three inputs.
Thanks!
Yes it is!
f.fields_for :identification do |identification_form|
identification_form.input :name
The above will give you:
<input type="text" name="act[identification][name]" />
This should then work with dates.
Edit:
I think you should be using date_select rather than input.

Getting Params from raw HTML components in Rails

I'm making an input form whose features don't fit so nicely into the general form_for template rails provides. I figured I would write my own HTML mimicking the html output of form_for, but embedded with other form_for elements.
For fields I could use the rails framework for I did. For the others I made hidden fields to store what was going to Rails, and regular input fields whose values I manipulated with JavaScript to put into the hidden fields.
This is one such field:
State:<br>
<input type="text" class = "state name_input administrative_area_level_1">
<div class="field">
<input type="hidden" name="address[state]" id="state">
</div>
When I send the value of the hidden field to the console, I get a good response:
state 37
Which means the state field holds the value 37. Perfect.
Except that when I try to make my model, the params come in empty:
"state"=>"",
I do have one field that works that isn't hidden:
Street:<br><input type="text" id="street" name="address[street]">
So I changed the state input type to number, which is what it would be if it weren't hidden, but same result. Why can't rails find my param?
You can show post more detail. I can't understand 100% things you do because you write pretty simple. But i guess that you need test params name is address[state] but it's not only state becase if only state of course it nil.
if html of you rendered same up then i suggest you should put one debug in controller to see params return controller when submit form.
For example: in controller you add line:
logger.debug params
to see right params that form send.
If your form is not tied directly to a object model, it might be difficult to use the form_for helper. In those cases you should use form_tag helper. Read more about form_tag helper here.
Refer this stackoverflow answer for the difference between form_for and form_tag helpers.
Hope this helps.

Rails - input default value from record

Perhaps I'm being silly but I've been trying to set a default value for an input with ruby on rails for hours and haven't cracked it.
I'm making a partial which can either allow users to create new records but will also show existing records if they exist. Code as follows
<input type="text" <%= (#prices.empty? || #prices.first.name.length == 0) ? 'placeholder="General admission"' : "value=" + #prices.first.name.to_s %> >
Which works perfectly for any value that exists UNLESS there is a space, for example, if price.name = "general admission" OR if price.name = "" (in which case it prints the placeholder) I get the following produced
<input type="text" admission'="" value="'general" id="event_price_name" name="[event][price][0][name]" aria-label="..." class="form-control">
It seems to get tripped up by the space.
Am I trying to use Rails in a way it wasn't designed to be done in? I'm more used to PHP which may be it!
Thanks
You should use the text_field_tag helper to build the input, this is preferred over building it with partial interpolation. Also, placeholder will automatically be overwritten if there is a value so you don't need to handle that part in the code, that's how it behaves by default on the browser.
<%= text_field_tag :admission, #prices.first.name.to_s, {placeholder: 'General Admission'} %>

Image with Checkbox instead of text

I have an association where a Vote has many Images, and I'd like this association to be checkboxes but instead of text have an image next to the checkbox instead of a text value the an example of the outputted HTML would look like:
<input class="check_boxes optional" id="vote_image_id_1" name="vote[image_id][]" type="checkbox" value="1"><img src="boat.png" alt="Big Boat"> <br />
Is this possible to do with simple form or will I need to write a custom field for this? If i have to write a custom field could someone recommend some good resources on making custom fields with simple form.
I can't test it right now, but I would do it more or less like this:
options = []
images.each do |image|
options << [image.id, image_tag(image.path)]
end
f.collection_check_boxes :votes, options, :first, :last
Maybe you can extract the construction of the options array to a helper method.
I hope it helps :)

Rails 3 and fields_for with activemodel (tableless) object

I have a tableless model that I'm trying to generate some form fields for.
The form looks like so:
= form_for :users, url: users_path do |f|
- books.each do |book|
= f.fields_for :books, book do |bf|
= bf.hidden_field :title, value: book.title
= f.submit "Send"
What I'm expecting to be generated for each field is something like this:
<input name="users[books][][title]" type="hidden" value="Some Book Title">
<input name="users[books][][title]" type="hidden" value="Some Book Title">
<input name="users[books][][title]" type="hidden" value="Some Book Title">
However, what I'm actually getting is
<input name="users[books][title]" type="hidden" value="Some Book Title">
<input name="users[books][title]" type="hidden" value="Some Book Title">
<input name="users[books][title]" type="hidden" value="Some Book Title">
Which means when the form is submitted only the last input field is available as the previous two have been overwritten due to them referencing the same thing.
This works ok when the model has an active record backend but not when it's tableless.
Any suggestions?
I think you need to add this to your users model
def books_attributes= attributes
# do something with attributes
# probably:
# self.books = attributes.map{|k,v|Book.new(v)}
end
And also define persisted? method for Book instance. Make it just to return false.
And add f for your fields_for in view:
= f.fields_for :books, book do |bf|
I hope this will work.
Welldan97 brings up a very important point. You need the persisted? method. I was getting an undefined method for the model name earlier. Check my gist out. It works, but not perfect by any means. https://gist.github.com/2638002
Right now this is pretty hard to do with Rails 3.x. That will change with Rails 4 with the advent of ActiveModel::Model which will give all the base methods for your model to be ActionPack compatable.
However until Rails 4 is released a good standard to make your model ActionPack compatible is the ActionModel::Model module itself. It "should" work with the current stable Rails. Check it out
How you choose to implement this is your decision, but I would probably just download the file and throw it in my application's lib directory. That way I could just include it using
class Book
include ActiveModel::Model
end
Easy Rails Form compatibility for custom models.
Try this:
f.fields_for 'books[]', book do |bf|

Resources