<%= text_field_tag :version, ApplicationController::VERSION %>
How to set common field with default value in each view in ruby on rails?
-
11-06-2023 - |
Question
I am new to RubyOnRails I want to set a common field for each view in ruby.
I am using Rails 4.0.0, I have set a global variable in controller.
VERSION = "1000"
I want to set a version field in each view.
<%= simple_form_for(@rebate, html: {class: "form-horizontal"}) do |f| %>
<div class="field">
<%= f.label :version %><br>
<%= f.text_field, version, :html_input=>{:value=>ApplicationController::VERSION} %>
</div>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I don't want to store this version field in database, but I want use this version field in each view. I want to check that value in controller while submitting the view. But I dont know how to set the common value for each view. For the above code I'm getting the error -
syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
Please help me, so that I can set common field in each view.
Solution
OTHER TIPS
Replace
## Notice comma after f.text_field that is causing error
<%= f.text_field, version, :html_input=>{:value=>ApplicationController::VERSION} %>
with
<%= f.text_field :version, :html_input=>{:value=>ApplicationController::VERSION} %>
By setting constant VERSION
in ApplicationController
, you would be able to access it in views,like the way you are doing.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow