How can I pass a boolean value in a form to a controller without the user being able to see or edit it? I'm assuming hidden_field is used for this, but how can I then assign a value to the variable?
Thanks for the help
-Pat
Pat,
I am slightly confused by what you mean with the 'but how can I then assign a value to the variable', but I'll give this a go.
First off, you are correct in the hidden_field bit.
<%= hidden_field_tag 'some_name', true %>
or, alternatively
<%= hidden_field_tag 'some_name', false %>
You get the point with that, I'm sure.
From there, in your controller, when the form is submitted you would get the value of that field like so:
some_boolean = params[:some_name]
Obviously variable names would be different, but that's the general gist of it all.
Good luck!
Related
i tried this here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
exactly this:
<%= collection_select(:sportlers, :sportlers_id, Sportler.all, :id, :name) %>
But there dont is written, how to get the selected value from the collection_select.
Im helpless, do you work with somethings like this?
I think collection_select will take the first two parameters, the object and the method, and use the values to make a hash, so in your case, you can access the values in your controllers like:
params[:sportlers][:sportlers_id]
I have another form_for select question.
I'm using a partial for my new and edit form for my Customer model. The :customer_type can be one of three values: Contractor, Business, Homeowner. So, I put these values in an array in my model.
def self.customer_types
customer_types = ['Contractor', 'Homeowner', 'Business']
end
In my form I do this:
<%= f.select(:customer_type, options_for_select(Customer.customer_types)) %>
This works fine in the new form, but on the edit form, how do I get the selected value for :customer_type to be selected? I've tried several things but nothing works for me.
Thanks for any tips.
-jc
options_for_select takes an optional second argument, which is the selected option :)
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
The second thing you need is the actual value, which can be accessed via f.object. So something along those lines
<%= f.select(:customer_type, options_for_select(Customer.customer_types, f.object.customer_type)) %>
I'm rendering a form with serialized attributes. The attribute serialization works fine under normal usage, but now i'm trying to serialize a hash. The view code looks like the following
<%= #item.seasonality_of_sales_pct.each do |key, value| %>
<%= eval("f.label :'seasonality_of_sales_pct[:#{key}]'") %>
<%= eval("f.text_field :'seasonality_of_sales_pct[:#{key}]'") %>
<% end %>
The error I'm getting is undefined method 'seasonality_of_sales_pct[:January]' for #<Item:0x007f01083edd38>. However, the line that is throwing the error is the second eval. The first eval evaluates just fine. I'm baffled as to why this might be happening.
In the controller, I am setting up an attribute like the following
#item.seasonality_of_sales_pct = {January: nil, February: nil, March: nil, September: nil}
Another question that could maybe be answered in the comments is: How bad does this code smell? I'm not sure how the rails community feels about metaprogramming like this. It hurts me a bit to do it, but seems to work most of the time
When you use form_for and then use f.text_field :some_attribute_name then the object you are building the form for (in your case #item) mush have an attribute named some_attribute_name.
You get this error because #item has no attribute or method named seasonality_of_sales_pct[:January]
I also would point out that there is no reason to use eval in your form, it is a serious security risk, as code can be injected.
I wanted to be a bit more thorough than Khaled's answer, which was sort of right. The reason that the first eval statement didn't cause the error was because f.label doesn't care what you give it. <%= f.label :fake_stuff %> will just create a label called Fake Stuff. I'm still not quite sure why the attribute didn't work. If I had f.text_field :seasonality_of_sales_pct, I got a text box full of my hash. Also, I got the labels to display the correct values.
I absolutely did not need to use evals here (I can hope it was only a moment of weakness). Just do
<%= f.text_field :'seasonality_of_sales_pct[:"#{key}"]' %>
Is there a way to send an extra parameter through a form in rails 3?
For example:
<%= form_for #post do |f| %>
<%= f.hidden_field :extraparam, :value => "22" %>
<% end %>
but lets say :extraparam isn't part of the post model..
I have an unknown attribute error in the create method of the controller when I try this, any ideas?
(I want to use the param value itself in the controller for some extra logic)
Call hidden_field_tag directly. See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag
These helpers exist for all the major form field types, and are handy when you need to go beyond your model's methods.
The following worked for me in passing extra parameters from the view back to the controller that were a part of my model and not part of my model.
<%= hidden_field_tag :extraparam, value %>
Example usage
<%= hidden_field_tag :name, "John Smith" %>
Ya Paul is right. Hidden_field is associated with your model whereas the extra _tag fields are not. I'm not sure of your needs but It's generally recommended in the RoR community to avoid passing a ton of hidden_fields like you might do in a php application.
Ive seen some code where ids were getting passed around in hidden fields which rails takes care on its own if you know the best practices and take full advantage of the framework. Of course I'm just saying this as general info as there are sometimes better ways at accomplishing the same functionality. Good luck on your apps.
I've got a form_for form, and within it, I'm using a form helper check box. The model has a field that's a boolean value, but I'd like to be able to display it on my form in terms of its converse since I think that's easier for users to understand.
Is there any way for me to have a Rails form helper act as if the model's boolean field is flipped?
Example:
<% form_for :user do |form| %>
<%= form.check_box :privacy_disabled %>
<% end %>
In this example, the User model has a boolean field privacy_disabled. I would like to display this in the form as check here to enable privacy.
The checkbox helper function has the ability to set its checked and unchecked values, but inverting these does not seem to properly populate the checkbox with the pre-saved value.
This is how I ended up solving this issue, I was actually close with my first attempt:
<%= form.check_box :privacy_disabled,
{:checked => !#user.privacy_disabled},
0,
1 %>
So the values returned by the checkbox are flipped, and whether it starts out checked is manually flipped as well.
Not the most elegant solution, but you could do something like the following:
Add these methods to your user model:
def privacy_enabled=(val)
self.privacy_disabled = val == "0"
end
def privacy_enabled
!privacy_disabled
end
Change the form to:
<%= form.check_box :privacy_enabled %>
Again, not elegant, but it works.