I have a field in one of my Rails forms that selects a "unit" and works fine initially. However when I go into the record the select defaults to another unit and not the one that was assigned, thus not retaining the value.
_form.html.erb
<%= f.select :unit_id, options_for_select(unit_select), {}, {:class => 'select'} %>
helper
def unit_select
Unit.all.map{|unit| unit.calls.empty? ? [unit.unit_name, unit.id] : ["#{unit.unit_name} (on call)", unit.id] }
end
The only thing I can think of is it's not retaining the value since the form field is using a map. I'm kind of newish to Rails and Ruby so I don't fully understand the constrains of my code yet.
Any help or suggestions is appreciated.
I was able to solve this by using options_for_select :selected => #call.unit.id
It then retained the previous selected value.
Related
I have overridden the standard Rails FormHelper to get it to spit form elements out in our (pretty much Bootstrap-based) format (after http://www.likeawritingdesk.com/posts/very-custom-form-builders-in-rails).
It works fine, but it is difficult to write a spec for. I am using a spec like this:
rendered_form = helper.my_custom_form_for(#account, :url => '/accounts') do |f|
f.inputs do
# This isn't rendered
f.text_field 'name', :size => 30
# This is rendered
f.select 'locale', options_for_select([%w(Australia en-AU), %w(UK en-GB)])
end
end
What is rendered includes the select tag, but not the text field tag. I suspect this is because of the way ActionView handles blocks now, appending the return value of the block rather than the return value of each statement in the block. Obviously, when used in context in an app, the helper method gets passed some ERB and evaluates in that context.
Am I right that it's not possible to test rendering helper methods in this way, with multiple statements in a block?
If I am right, what would be the least hacky way of making a spec that does the same thing as the helper does in the context of a full app? Make an ERB string and somehow pass it to the helper to render?
Did you solve this? I am curious if you are just getting the last statment in the block returned. Have you tried changing the f.text_field/f.select order to see what gets returned? If so, then you just need to add them:
rendered_form = helper.my_custom_form_for(#account, :url => '/accounts') do |f|
f.inputs do
f.text_field('name', :size => 30) +
f.select('locale', options_for_select([%w(Australia en-AU), %w(UK en-GB)]))
end
end
I have a Rails app that pulls in music from Soundcloud. This data contains a title, which I save as mix.sc_title but it's not always properly formatted. I have added an additional attribute on my Mix model which I call mix.override_title
For display on my site, I want to use the override title if available, and the sc_title in all other cases.
I have a Mix model method to do this for me
def display_title
override_title.blank? sc_title : override_title
end
Mixes#index grabs #mixes = Mix.where(:active => true) and mixes/index.html.erb looks like this:
<ul>
<% #mixes.each do |mix| %>
<li><%= link_to mix.display_title, mix %></li>
<% end %>
</ul>
As you can see, I'm not directly using any mix attributes, and so I take a huge hit when I go to the DB, and I don't actually benefit from it.
Is there a leaner way to get just the information I need? (mix.display_title)
I have tried Mix.select("display_title").where(:active => true) but it fails because display_title is not a real DB column
You can do Mix.select("sc_title, override_title").where(:active => true) and it will work, since those are the actual fields that the method uses. I don't really think getting the additional attributes gives you that much of a DB hit but sometimes selecting only what you need can be beneficial.
As you start chaining on more Arel commands, consider putting the select into a model method:
def select_active_titles
select("sc_title, override_title").where(:active => true)
end
Edit: Your link_to helper also secretly calls mix.id to link to the right mix, so make sure it's working and if not add id to the list of selected attributes.
This is long so I hope you'll bear with me...
I have a model called Update with two subclasses, MrUpdate and TriggeredUpdate. Using single-table inheritance, added type field as a string to Update.
In my view I'm checking which type it is to decide what to display. I assumed since type is a string, I should do
<% if #update.type == 'MrUpdate' %>
This failed, i.e., it evaluated to false when the update was an MrUpdate. I noticed that at this point, #update.type.type is Class. OK, whatever, thought I, so I changed it to:
<% if #update.type == MrUpdate %>
and it worked, i.e., the comparison evaluated to true when the update was an MrUdpate. Then I did it again lower down in my view and it failed again (i.e., it evaluated to false when the update was an MrUpdate.)
Turns out the culprit is a couple of <%= link_to ... %> calls I use and make into buttons with jQuery. If I put this code in my view:
<br>
<%= #update.type.type %><br>
<%= #update.type %><br>
<%= link_to 'New Note', new_note_path(:update_id => #update.id), :class => "ui-button" %>
<br>
<%= #update.type.type %><br>
<%= #update.type %><br>
What I see is:
Class
MrUpdate
(the New Note button)
String
MrUpdate
It's changing from a class to a string! So what the heck am I doing wrong or missing here? Why should a link_to do that? First I'm not clear why it's not a string in the first place, but then really confused as to why it would change...?!? Any help or explanation would be helpful. I can just code it one way at the top and another way at the bottom, but that way madness lies. I need to understand why this is happening.
I figured out what the issue is here. Thanks to fl00r for pointing the way.
Yes, type is a reserved in Ruby 1.8.7 which tells you the class of the object you call it from. But it's also true that it is the name of the field used in Rails to indicate single-table inheriance and to store the name of the class of each instance of the subclass.
So I naively tried to access the value of the type field using #update.type. But what this was doing at the top of the view was calling the type method of the Object class in Ruby. For whatever reason, after the link_to calls, it was then access the value of the type field of the updates table.
While trying to figure this out I called #update.type in the Rails console and saw this message: "warning: Object#type is deprecated; use Object#class". Finally it registered what I was doing. When I changed my calls to:
<% if #update.class == MrUpdate %>
everything works as expected. I never saw a call to determine the type in any of the pages I found via Google about STI. This despite the fact that they all recommended using only one controller, wherein sometimes you must need to determine the class of the instance you have.
So, dumb mistake--pilot error. But maybe this will help someone else who gets tripped up on this.
I have a GroupCoach model, Group Coaches has_many :groups. On my new Group form I want to pass a group_coach_id to the Group object in a hidden field so that a group gets associated with a GroupCoach without the user having to select one.
So in my Groups_Controller
#group = Group.new
#group_coach = GroupCoach.first(:order => "RAND()")
This will get a random GroupCoach. and then in the new Group view I have a hidden field
<%= f.hidden_field #group_coach %>
This obviously doesn't work 100% right. It does pass the group_coach_id but its not telling the form what column to save it in...
I have also heard this is very insecure...
Make a token column. Simply SHA1 encrypt it (or whatever your choice is) and pass that instead. It's much harder to guess.
I used the following code to resolve this issue
<%= f.hidden_field :group_coach_id, :value => #group_coach.id %>
But is this the most secure? Seems pretty insecure as I could change the value in Firebug or something...
I've got the client_side_validations gem working really great on my forms. I'm using Devise for my users model and when I go to the edit_user_registration form, validations are thrown up for everything except the :current_password, :password, and :password_confirmation fields.
Fort instance is I leave the email blank, the validation pops up right when I tab out of the field. However, if I leave the current_password field blank and tab out of it nothing happen, then when I submit the form I get the error "1 error prohibited this user from being saved: password can't be blank"
Thanks for any help
http://pastie.org/1907464
Currently ClientSideValidations will filter out any conditional validators. Devise sets some of the validators as conditional: https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb#L24-32
The reason I did this is because there is no good way for the client to determine the true value of that conditional. I could do it at the time the form is generated but what if that conditional relied upon a value that could be changed on the form? So I opted to filter them and let things fall back to the server.
That was the idea but clearly it has imposed unfair limitations on some things. This being the most obvious (and popular).
So I plan on releasing a new version very soon that will allow you to explicitly override the conditional filters. It will work like this:
<%= f.text_field :password, :validate => { :presence => true, :confirmation => true } %>
or
<%= f.text_field :password, :validate => true %>
In the first case you can choose which validators to turn the filter off. In the 2nd case it will turn the filter off for all validators on that attribute. The conditional will be evaluated at the time the form is generated and if it passes it will add the validator to the input element for use on the client.
The master branch now supports this format. Point your Gemfile to it and you should be good
It’s simple! The gem extends the Rails default form builder, and all you have to do is set a :validate => true option on any form_for (or simple_form_for for simple form users) tag that you want the inline validations for. The form builder uses some rails reflections on your model validations to generate some json that gets included in a script tag after your form. The json is then used by the gem’s Javascript framework to perform the validations that need to be performed.
<%= form_for(#user, :url => registration_path(resource_name), :validate => true) do |f| %>