I'm using the simple_form gem.
I'm rendering an input based on a collection (a list of all my actiontypes)
<%= f.association :actiontype, collection: Actiontype.all, input_html: { data: {'impacts-pnl' => ??}} %>
I would like to be able to add a data-attribute to the input to store extra data.
In this case, I want to store the impacts_pnlattribute of my actiontype. The only problem is that I don't know how to refer to the current actiontype
collection.impacts_pnl doesn't work (obviously)
actiontype.impacts_pnlneither.
how can I pass this extra bit of data to my input?
If you want to add these attributes to the option-elements of a selectfield, you should alter the collection by using for example the .map() function. Also, use the input helper with block to do this, otherwise it doesn't work;
= f.input :actiontype do
= f.select :actiontype, Actiontype.all.map{|a| [a.name, a.id, {"data-impacts-pnl" => p.impacts_pnl}]}
For more information about this issue, see https://github.com/plataformatec/simple_form/issues/188
Related
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.
I am trying to pass an extra variable, that determines whether or not the user has clicked on a particular checkbox, and this variable is not a part of my model. I want to make it so on the controller update function, it can have access to this variable, and see what it was set to. I have seen some other stack overflow answers for this type of problem, and it is generally suggested to do something using hidden_field_tag, something like this:
<% hidden_field_tag "blah", params[:test] %>
or
<% hidden_field_tag :example, "test" %>
When trying this, I did a params.inspect and could not find the "test" param variable, using both of the above options. Should I be trying to retrieve this hidden field tag in a different way? Will it be available in the update request to the controller? If not, does anyone know some way this is possible?
Open to any suggestions,
--Anthony
You would either do it like this
<%= hidden_field_tag 'test' , 'blah' %>
or you could do this
<%= hidden_field_tag :whatever_you_want , 'blah', {:name=>'test'}
You had the name and value reversed in your post. The really important thing is to make sure the form element has the name you want to show up in the params hash. The second example would generate the element with id='whatever_you_want' name='test'.
I search for a working solution for a rather simple problem, but could not find a good explanation.
What I currently have (working) is an index view which contains:
a form to enter a new element and
a paginated list of existing elements (using will_paginate).
For the list I am interested in only part of the data, thus I am trying to add a form with filter options and I would like to store the forms content in a cookie (which should be replaced with an per user object stored in the database, but i do not have users yet). What I cannot figure out is how to get the values from the form stored in a cookie (and vice versa) and how to use it together with will_paginated.
What I currently tried to do as a first step is to create an #filter object in my controller and adding the filter form for this object, setting the form options to use the index controller again. This leads to selected filter parameters passed in the params hash to the index controller (visible in the url). But this solution has some drawbacks. first the filters are gone as soon as I change the view (e.g. by creating a new element) and second the #filter object should be the cookie instead.
Here is the code I have so far:
View-partial for filter:
<%= form_for(#filter, :url => {:action => "index"}, :html => {:method => :get}) do |f| %>
<div class="field">
<%= f.label :german %><br />
<%= f.check_box :german %>
</div>
<div class="actions">
<%= f.submit "Filter" %>
</div>
<% end %>
Controller:
def index
#word = Word.new
#filter = Word.new(params[:word])
#words = Word.paginate(:page => params[:page]).order('word')
# ....
Can anybody help me? How is such a functionality (filtering results) done in other applications?
So the answer to the question is, use a where clause to include only the matching records in your result.
#words = Word
.where("german = ?", params[:word][:german] != 0")
.order('word')
.paginate(:page => params[:page])
This is a new Rails syntax called Active Relation (AREL for short), which should generally replace the older find and find_by methods. It has several benefits that can improve performance, notably that the SQL it executes (which you can see in your logs) only occurs when it is referenced, not when it is declared. This give you neat ways of defining partial relations (even as named scopes) that you can build up to create simpler statements that combine together.
The order of the various clauses doesn't matter -- AREL will generate the same SQL, but generally I like to follow the order of the underlying SQL,
where
joins
group
order
limit
offset
(limit and offset are handled in your case by the pagination tool).
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...
How can I disable automatic conversion of HTML tags in Rails3? I have output in some controller view. For example I have method which outputs simple HTML link set..
[:en, :de].map{ |locale| link_to locate.to_s.upcase , { :locale => locate } ...
In view I'm calling my method <%= my_method %>
As a result I get this:
| <a href="/login?class=language_selected&locale=en">EN</a>
How can I disable it?
I haven't worked with Rails3 so no guarantees. but it looks like this has to do with the fact that your method returns a list.
Rails will usually format internal data structures for output by escaping the special characters and displaying the html escaped interpretation of your data.
Try tacking a .join onto the end of your map call to return a string
[:en, :de].map{ |locale|
link_to locate.to_s.upcase , { :locale => locate }
...
}.join("<br/>")
Also rwilliams aka r-dub's suggestion to use raw will probably be necessary addition to this code. raw on a list however may give you an undesirable result probably because of an internal to_string call. Which is an implicit join(""). So add the raw to the method call in addition to returning a string.
<%= raw my_method %>
If you're sure your methods output is safe then you can use the raw method.
<%= raw my_method %>