Rails: Model property, value is not displayed on view mode - ruby-on-rails

I have a model but one of its property is not displaying its value.
<% #medical_history.neonatalcourse %>
I already check my sqllite file and the value is there. So I am sure that I am saving the field. The problem is it not displayed.
Thanks

you need to use an equal sign with your tag to tell it to print the output of the ruby statement into the view. The version without the "=" is for when you don't wish to have a statement print its value into the page.
<%= #medical_history.neonatalcourse %>

Related

Storing text with line break in javascript array

I have a form where user can enter a note via a textfield.
When entering the note, user can press 'enter' to add line breaks and also the note entered can consist of multiple lines and it is stored in a table column of type varchar(2000) latin1_swedish_ci
On some other page, I am retrieving the notes from the database and storing them in an array. I am alerting the array contents to test whether everything is ok
My problem is that if the note spans on more than 1 line or contains line breaks, it does not seem to be stored in that array and the alert function does not alert anything for that particular note.
However, it is displayed properly if i display it using plain ruby code.
Here is an example of my code:
/********** loop through the resultset and store the values in the test_array **********/
<% #saved_note.each do |note| %>
<script type="text/javascript">
testarray[note_counter] = '<%= note.value %>';
alert(testarray[note_counter]);
note_counter++;
</script>
<%= note.value %>
<% end %>
Any suggestion is most appreciated.
Thanks a lot
If note.value spans more than one line, you will have this result:
testarray[note_counter] = 'Dear tanya,
how are you doing today?
sincerely,
user';
The problem here is that javascript doesn't support multiline strings without some massaging. If you've got any kind of newline characters hanging out in note.value, you'll want to escape them (see this example) before printing them out.
You can try
testarray[note_counter] = <%= raw note.value.to_json %>;
Personnaly I store that in AplicationHelper
def json value
raw value.to_json
end
and then in your view
testarray[note_counter] = <%= json note.value %>;
I'm unable to find an exact example of how I dealt with that in php, but the idea would be to try to parse all the symbols to their ansi code and check for the needed symbol like that. Just an idea tho.

Rails View: Accessing text field elements

I am a newbie to Ruby on Rails. And I am working on a project. In my view, I am asking the user to input some data using text_field_tag. I know you can access the data entered in the controller using params[]. But how do I access the value the user has entered in a different part of the same view? I tried searching but I was not able to obtain an answer.
You can call params system all of your view like in your controller so you can use it.
<%= params[:my_params] %>

Ruby on rails: Avoid drop down if values are already selected

I am pretty new to Ruby on rails. I have one query
I have one view (say view1) in which I am showing a drop down. For that I have passed and array and populating the drop down using that array like dis
<td><=% select_tag, options_for_select(#businessApprovers)></td>
So when I submit the form it goes to an action which intrun renders another view which has 5 tabs in body and each tab has a partial view. one of them calls my previous view view1. Now when it calls the view1 it again shows the drop down. instead it should show only one value and that too non editable.
we are having some other drop down too but they have hard coded values. We are doing that like this:
<td><=% f.field :contries, :condition_select, [abc,pqr] ....
and above thing is working fine. For that it is not showing drop down.
So I wanted to know how to avoid that drop down. Also what is the use of "f.field" because I removed that and from then on it is causing this problem.
Firstly, field is for CSV, text_field is a thing. Is that what that should be? Also, I'm hoping that's not exact code.
Instead of:
<td><=% f...
It should be:
<td><%= f...
That aside, if you're simply looking to display a select field when no selection has yet been made and simply text when it DOES have a value, then it would be as simple as using a conditional:
<% if thing.something.empty? %>
<%= f.select ... %>
<% else %>
<%= thing.something %>
<% end %>
If my assumptions were incorrect, please reply and I'll revise.

ASP.NET MVC2 Html.Checkbox not checking when isChecked=true

I have several checkboxes on a form. Only one of them is checked when it needs to be; all the others are unchecked regardless of whether the isChecked parameter is passed in as either true or false.
The checkboxes are coded like this:
<%= Html.CheckBox("cbName",Model.checkvalue)%>
<%= Html.CheckBox("cbName1",Model.checkvalue1)%>
I have stepped through the code and Model.checkValue and Model.checkValue1 are both true, but cbName is not checked and cbName1 is checked (in fact, in my actual app' there are several more CheckBoxes and none are checked -except the second one in the form- although the Model properties are all true in the test I ran).
Has anyone come across this (mis)behavior before & can you let me know where I am going wrong, please? I can't find a similar question anywhere, so I am hoping I am just making a simple error that will be quick to fix...
what about use different way of rendering the code ( of course you can later simplify this code or create your own helper):
<%
if(Model.checkvalue1){
%>
<%= Html.CheckBox("name", new {checked =checked }) %>
<%}else{%>
<%= Html.CheckBox("name", null) %>
<%}%>
idea 2
make sure that the value you are passing in is boolean: therefore cast is as boolean
<%= Html.CheckBox("cbName1",(bool)Model.checkvalue1)%>
idea 3
before using the code
<% bool myTempValue = Model.checkvalue1; %>
<%= Html.CheckBox("cbName1",myTempValue)%>
There could be some reasons for this behavior:
Is there any query string parameter named cbName in URL of the page? Or, is it a POST request of the form? Query string and form (POST) data take precedence over explicit values set in code.
What browser are you using? FireFox sometimes preserves form data over page reloads. If you check a checkbox and refresh the page, FireFox checks the checkbox again, even when there is no "checked" attribute in HTML input element.
It was because the underlying data uses a nullable boolean.
I switched the CheckBox to a CheckBoxFor and got the error outlined in this question and this told me that the problem is the fact that the underlying data currently uses a nullable boolean. Since this data type will be switched for a not null boolean once ready I don't need to work around this.
I hope this answer helps someone else.
Thank you for everyone's contributions.

Rails: Set a temporary variable?

I need to set a temporary variable (actually more of a "true" or "false") for a view.
The use case is that when a user is created, they are redirected to a dashboard page. For Google AdWords conversion tracking, there is a bit of code that needs to be displayed in the view, but it only should be displayed after the create method has been run.
So I'm guessing the way to solve that is to set some temp variable (#show_conversion or something) and set it for one view (similar to a flash message).
So, how do I do that?
<% unless #user.new_record? %>
<!-- do stuff in here -->
<% end %>

Resources