Select, assign a variable, and link_to - ruby-on-rails-3

I have a language selection that is very simple : French or English. If I want to add more languages, then I should have a select so that it does not take a full screen width !
Here is what I currently have:
<div id="language">
Language:
<%= link_to_unless_current "English", locale: "en"%> |
<%= link_to_unless_current "Français", locale: "fr"%>
</div>
I can't find the correct syntax to place this in a select box. Any idea ?

You can use the select_tag helper.
You might also look at:
options_for_select
and
select

Related

Syntax for using variable in the view of a rails application for active record query

I've been looking for a way to properly do this for a couple of days now and haven't been able to find any posts or information yet as to how it should be done. Basically, I've got this code in one of my views:
<p>
Solo Payout Age <%= i %>:
<%= #product.solo_payout_factor_age_THIS SPOT SHOULD BE THE VALUE OF "i" %>
</p>
The variable "i" was declared earlier and is working just fine in the line above the active record query. I need that value to be part of the active record query too, though, and haven't been able to successfully get it to run.
I've tried all sorts of combinations of the "#{i}" method to insert the variable, but no luck.
Thanks in advance for any assistance with this.
<p>
Solo Payout Age <%= i %>:
<%= #product.send("solo_payout_factor_age_#{i}")%>
</p>

Bootstrap typeahead avoid other value

i still try in rails 3.2 to use bootstrap typeahead, it works fine, but how can i avoid users to type anything in text field.
I code looks like this
<%= text_field_tag(:text, "", :data => {:provide => "typeahead", :items=>"4", :source=>'["Swiss","German","English"]'}) %>
users may only type Swiss, German and English in the text field, but not for example Chinese.
How can i validate the value in text field?
Set tag attribute disabled="disabled"

How to display text in erb file?

I started rails a week ago, I want to know that how to display an unmodifiable text using erb file,
<%= f.label :email %>
<%= f.text_field :email %>
Here, instead of a text-box, I need a simple text displaying email id.
Any clues?
By "unmodifiable text" in a text field, I'm assuming you don't just want to have the email displayed on screen (if so, then Yuri Barbashov is correct) but also want it inside an input text field, but be unmodifiable.
In that case, you might be best off using javascript. Have a look at this StackOverflow answer for some JQuery examples.
Edit:
Actually, the solution here may be easier if you never plan on re-enabling the text field:
<%= f.text_field :email, disabled: true %>
<%= #your_model.email %>
But to be more precise you have to show your view code

Rails3 form_for hidden_field undefined method 'merge'

My attempt to place a hidden_field within a form_for is crashing within cucumber on an ActionView helper error. Something also about FixNum which escapes me since I haven't dug through the source code. My prices_controller shows this:
#price = Price.new
#commodity = Commodity.find(params[:id])
I want to make the link between price and commodity with this hidden_field:
<%= form_for (#price), :url => prices_path do |f| %>
<% f.hidden_field :commodity_id, #commodity.id %>
.
.
<div class="actions">
<%= f.submit "Submit" %>
</div>
Looked at the form_for api and the above should work. Reading other replies on stackoveflow, I have put the hidden_field in its own div within the form, added a Hidden_field_tag, and placed it within the actions div before the submit line. Looking at the merge msg, I guess it doesn't like something about the line, but it appears OK to me. The commodity_id field is the match field, sam
If you could paste the error message itself, and the relevant lines of the trace, it could help us. Right now, the only thing I see is that the ERB tag before f.hidden_field should be <%=, and I'm not sure about it since I don't use ERB. For what it's worth, merge is usually used with Hash objects. Maybe it can point you in the right direction
EDIT Ok I get it. You have to write f.hidden_field :commodity_id, :value => #commodity.id.

tiny mce display tags in rails 3

I am trying to store content of tinyMCE into "detail" coloumn.
Now when I display the content it displays wit all the <p> tags <i> tags etc.
This Is a security feature in rails3 .
But I don't want the <p> tags to be displayed , I want it to be rendered as HTML.
One way I found was <%= something.detail.html_safe %>
the other way I thought was to create a function in model like
def detail_safe
return self.detail.html_safe
end
and display using <%= something.detail_safe %>
Either ways I need to change the <%= %> tag at many places. Is there an easier solution? Or should I manually change at every place?
Thank you.
In the model:
def detail
self[:detail].html_safe if self[:detail]
end
Please note that you will always get html_safe output in this case when you do model_object.detail.
Not matter how you do it, you will have to change all of your <%= %>.
Your options are:
<%= something.detail_safe %>
<%= something.detail.html_safe %>
<%= raw something.detail %>
The only other option I can think of is turning off XSS protection - but don't do that!