Translating label for references collection_select - ruby-on-rails-3

i have form with one selectbox (collection_select) for references column. What is proper way to translate label for this widget? For all others a use default Model.human_attribute_name (my code, Translations for Active Record Models)

If you use in your [language].yml file:
attributes:
my_model:
property: 'translatet property label'
[...]
You can simply use the following in your view:
<%= form_for([...]) do |f| %>
<div class="field">
<%= f.label :property %><br />
<%= f.collection_select [...] %>
</div>
[...]

Related

How to display dynamic content in rails label without using javascript

I can't find how to display a dynamic label in Rails, i've tried using the :value => show_name property but it didn't work, it only displays Show name. Here is the view code
<p>
<div class="control-group">
<%= f.label :show_name, :value => :show_name, :class => 'control-label' %>
<%= #this next line fails with undefined method `show_name' for #<ActionView::Helpers::FormBuiler>
#f.label f.send :show_name, :class => 'control-label'
%>
<div class="controls">
<%= f.text_field :variable_value, :class => 'text_field' %>
<%= f.hidden_field :variable_id, :class => 'text_field' %>
<%= f.hidden_field :show_name, :class => 'text_field' %>
</div>
</div>
<p>
and if needed here is the show_name definition inside my model.
def show_name
Variable.find_by_id(self.variable_id).name
end
Ok, so i end up finding a solution that is very DRY, thank to this post. And the only thing im going to do is explain a lit bit more what to do:
First we are going to asume the most complex case in which we have nested forms and so we are using fields_for inside a form_for method:
<!-- f represents the form from `form_for` -->
<%= f.fields_for :nested_model do |builder| %>
<p>
<div class="control-group">
<!-- here we are just calling a helper method to get things DRY -->
<%= builder.label return_value_of_symbol(builder,:show_name), :class => 'control-label' %>
<div class="controls">
<%= builder.text_field :variable_value, :class => 'text_field' %>
<%= builder.hidden_field :variable_id, :class => 'text_field' %>
</div>
</div>
</p>
<% end %>
Note that we included the builder object(as specified in the fields_for call) in the parameters of our helper.
In our helper we define the return_value_of_symbol function
def return_value_of_symbol(obj,sym)
# And here is the magic, we need to call the object method of fields_for
# to obtain the reference of the object we are building for, then call the
# send function so we send a message with the actual value of the symbol
# and so we return that message to our view.
obj.object.send(sym)
end
Use label_tag, put the show_name on a instance variable on your controller and use like this:
<%= label_tag #show_name, nil, :class => 'control-label' %>
EDIT:
On your application_helper.rb, create a helper method similar to this one:
def show_name(name)
content_tag(:label, name, :class => 'control-label')
end
Then you can use the show_name(name) on your views like this:
<%= show_name(#name) %>
Just remember to populate the #name variable.

Pass a variable to text_field without changing form

I have _form.html.erb
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :password %>
<%= f.password_field :password %>
Now if I render this form in homepage, HTML code should be:
<label for="session_name">Name</label>
<input id="session_name" name="session[name]" size="30" type="text">
...
If I change my _form.html.erb to:
<%= f.label :name %>
<%= f.text_field :name, disabled: true %>
...
HTML code should be:
...
<input disabled="disabled" id="session_name" name="session[name]" size="30" type="text">
...
But, I don't want to change my _form.html.erb, so how can I pass the disabled: true into my form? I tried to use render partial: but don't know syntax.
I just learn Ruby on Rails for 2 weeks, so please help me.
i donno why u wnt to do so but u can solve the prob by this method. Try doing-
in application helper, add method -
def add_disabled
#i suppose that u want the field to be disabled in the users controller for edit action but not for other controllers or actions
return "disabled='disabled'" if params[:controller] == "users" and params[:action] == "edit"
end
in _form.html.haml
= f.text_field :name, #{add_disabled}
this will call the helper method and return "disabled='disabled'" depending upon the controller and action

RoR: how can I get a micropost to be created by two seperate micropost forms according to an element?

My microposts have a column called type which is a string. This can either be purchase or sale. I want two seperate input forms, where if you input content into one then it automatically fills in purchase as the type (when creating the micropost) and if you input content into the other it automatically fills in sale. .
Here is my form as it is
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
I recently added type as a column in the micropost data table. That is why there is no input for it (yet). Read above for how I want the type to be automatically filled in
Im thinking one way to do it is to id each form with something. Then when the form is being filled in, I could somehow tell it to automatically fill in the hidden type field based on which form is filled in. IS this possible??
I think you are referring to the hidden_field here. Use hidden_field to pass information to the controller without presenting it to the user. Here is one form for the purchase:
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field :type, 'purchase' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and another for the sale:
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to sell?" %>
<%= f.hidden_field :type, 'sale' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

Custom model not working in rails 3.2.2

I am a newbie in ROR, so please help me with this.
I created a model named 'Customer' from rails console, in the database there is a table named 'Customers' and it has columns: id, name, address, gender, dob, credit_card, created_at and updated_at. So, to save these information I created a view such as:
<%= form_for(#customer) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :dob %><br />
<%= f.date_select :dob %>
</div>
<div class="field">
<%= f.label :gender %><br />
<%= f.text_field :gender %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :phone_number %><br />
<%= f.text_field :phone_number %>
</div>
<div class="field">
<%= f.label :credit_card %><br />
<%= f.text_field :credit_card %>
</div>
<div class="actions">
<%= f.submit 'Order', :action => :save_order %>
</div>
this view would be called by 'public' controller, from action 'check_out'
the code for action check_out is as:
def check_out
#customer = Customer.new
end
and code for action 'save_order' would be:
def save_order
#customer = Customer.new(params[:customer])
if #customer.save
#somcode
end
end
but i am getting a problem in the view part, "undefined method `customers_path' for #<#:0x9f49814>" in the line which contains "form_for(#customer)"
what i am doing wrong ? I am using rails 3.2.2
Thanks in advance :)
You don't have route for customer controller actions. Just add this line to routes.rb file
resources :customers
This one will generate several helpers, one of them will be customers_path. You can list all path helpers by command
rake routes

Rails: Accessing data of other SQL table in View

I don't understand how access data from my View in other table.
I have few models. First is User, second is UsersPreferences. So i have settings page for users and code looks like this:
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
...
<div class="checkbox">
<%= f.check_box :notification_about_new_followers %>
<%= f.label 'Notify via email about new followers' %><br />
</div>
This code works (User has row notification_about_new_followers) but i want to change it. I decided that better way is keep preferences in other table. So i created model UsersPreferences which has user_id and flag 'notify_about_new_followers'. I tried to rewrite my view like that:
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
...
<div class="checkbox">
<%= f.check_box UsersPreferences.find_by_user_id(#user.id).notify_about_new_followers %>
<%= f.label 'Notify via email about new followers' %><br />
</div>
But when i visit page with code i get such error
undefined method `true' for #<User:0x007f8ac180dee8>
How can i solve this error?
User model:
class User < ActiveRecord::Base
attr_accessible :name, ... , :notification_about_new_followers
...
has_one :users_preferences
end
UsersPreferences model:
class UsersPreferences < ActiveRecord::Base
belongs_to :user, :class_name => "User"
attr_accessible :notify_about_new_followers
end
This line:
<%= f.check_box UsersPreferences.find_by_user_id(#user.id).notify_about_new_followers %>
is causing the error. UsersPreferences.find_by_user_id(#user.id).notify_about_new_followers is true, so essentially that line reads:
<%= f.check_box true %>
And like the error says, there is no 'true' attribute for your UserPreferences model.
To fix this you'll need to first read up on MVC (Model-View-Controller). Without knowing how MVC works, you'll never be able to move forward with Rails. There are a lot of great resources out there, just google "Model View Controller for rails". Here is a good one. A little goofy, though... if that's not your style, try another link.
After you have learned that, look up the rails helper fields_for, and there you will find your answer.