Displaying data from the console into an HTML form using EJS - html

So, I'm building an app where the user enters some data, the code modifies it, and returns it back to the user on the screen in an HTML input tag probably.
The user input is taken as : const userInput = req.body.user_word; (using Node.js for backend)
and the modified data is stored as var encrypted_word
I wish to display the contents of this variable into an input tag <input type="text" name="encrypted_word"> using EJS
I read that <%= tag is helpful in doing so but couldn't devise the solution.
Can someone help me with this...

i would use <%- instead of <%= also you have to the attribute value for inputs
<input type="text" value="<%- encrypted_word %>">

Related

How to access required element of HTML in shiny

I am working on form where i am taking inputs from users and all fields are mandatory and have validations like valid emailid, only 6 digit pincode.I have created a form on HTML and all validations are working fine on HTML by using "required" element of HTML which makes input type mandatory also input type email of HTML put all the validations required for an emailid.
But i want to achieve same thing in shiny internal UI form i tried a lot by accessing html tags inside shiny but everytime i am getting error for required element that i placed inside input tag of shiny.
Below attached image is from HTML form that i created using raw HTML but i want to achieve same thing in my shiny internal form.
Code for the above image:
<input type="email" name="emailid" value="" placeholder="Enter valid email id" required>
Can anyone help me how to achieve the same.Any help would be appreciated!
My solution to this would be in two parts. First, I'd put an observer on input$emailid to check that the user has entered a valid email address. If they haven't I'd then use the shinyFeedback package to display a pop-up prompting the user to put things right. You could also use shinyFeedback to display the prompt you show in your screen grab when the input is empty, but my own personal opinion is that that would be overkill.
Something like:
library(shinyFeedback)
observeEvent(input$emailid, {
feedbackDanger(
"emailid",
!isValidEmailAddress(input$emailid),
"Please enter a valid email address"
)
})
To get shinyFeedback to work you need to add useShinyFeedback() at the start of your ui page. Furter details here. Note that isValidEmailAddress() is a function you'll need to write yourself.

How can I send values from an erb file to a ruby controller?

I'm trying to create a dynamic quiz system as follows:
A controller passes a value to an erb file (which creates an html
form with the first quiz question)
The erb file has an html form where the user can enter an input
The erb file sends the input back to an action in the controller
The action checks the input value and loads the next question. The next
question depends on what the user's input was.
Now I know that I can send a value from an action to its corresponding erb file. Basic stuff. But how can I send a value from the erb file back to the controller (so that it can process the input from the html form)?
For example, this is what the html template (question1.html.erb) for the first question looks like:
<head ><title >Question 1</title ></head >
<body>
<form action="localhost:3000/welcome/processQ1" method = "get">
<p>What is the sum of 5 + 6 ?</p>
<input name='sum' type='text' width='5' id='sum' required />
<input type='submit' value='Submit' />
</form >
</body>
What I want is to send the value of 'sum' back to an action called "processQ1" in the controller. How can this be achieved?
First of all, try always use the Rails way instead html pure tags for create your forms:
<%= form_for :quiz do |f| %>
<label>What is the sum of 5 + 6 ?</label>
<%= f.text_field :answer %>
<%= f.submit "Submit" %>
<% end %>
After submit, in your controller you will get the value of answer in your ProcessQ1 (This is not a good name for a controller because they are written in plural and more generically. So, for your purpose, QuizzesController is an option) in params rails hash variable on your action create.
ex: { quiz: {answer: 5 }} and make anything you want with this data.
Please, read this docs. It is a good way to understand this processes of framework.
http://guides.rubyonrails.org/form_helpers.html
http://guides.rubyonrails.org/getting_started.html
http://guides.rubyonrails.org/getting_started.html#saving-data-in-the-controller

How can I include an HTML input in rails strong params?

I'm trying to create instances of a model "Post" with strong params that look like this:
private
def post_params
params.require(:post).permit(:name, {images: []})
end
In my form, this is how I upload files to post_params["images"], and therefore to post.images:
<%= f.file_field :images, multiple: true %>
But I want to apply custom styling to the upload button, so I build one using HTML. But I can't figure out what name attribute to give the input so that it's included in post_params. Here's what I've tried:
<input name="images" type="file" multiple="true"/>
<input name="post[images]" type="file" multiple="true"/>
<input name="post['images']" type="file" multiple="true"/>
<input name=":images" type="file" multiple="true"/>
But they all just go to params["post"]["images"], not strong_params.
What name can I give an HTML input so it's included in strong params?
Something like this:
<input multiple="multiple" name="post[images][]" id="posts_images" type="file">
This should generate the correct JSON params that your controller is expecting. It can get confusing with the multiple levels of hash/array. That's why I use the inspect element in the browser as mentioned in my comment above to see what Rails is generating, then I can build custom styled elements to suit my needs.
One way to remember it is: You are require-ing post, and then looking for a hash with a key of images and a value of type array. That way it's easier to know where to use singular vs plural, what is the key, etc. So mapping it out syntactically would be:
<model name>[<key defined in strong params>][]
in the .erb version if you look at the top of your form, think about what the value of f is in the ... do |f|. That's where it gets the controller name of post (singular) and then you are giving it the method name :images (plural) in the file_field. According to the Rails Docs you could use the singular :image but the plural seems more correct to me.

rails parameterize form data

I have a rails application that allows searching of posts when a get request in the form of /posts/search/:searchstring is recieved. I have no problems with the functionality when I put the search string in using my browser's url bar, but I am stuck trying to create a search form.
How can I make an html form in rails that allows me to send a get request using the above syntax? Am I going to be stuck using a controller method to redirect? I really want this functionality because I am a firm believer that the URL is a part of the UX and I hate ugly URLs like this:
http://example.com/search?utf8=%E2%9C%93&q=searchstring&commit=Search
A form's gonna do what a form's gonna do, and if you have a form using GET, it's gonna serialize those parameters into the query string.
But since you are using get, you also don't truly need to submit the form. Rather you can just send the browser to the URL.
So for example, somewhere in your JS:
function doSearch(form) {
// This assumes that the first input of your form is the search
// box. YMMV.
var query = encodeURIComponent(form.elements[0].value);
// just send the browser to the constructed URL
window.location = form.action + "/" + query;
// and return false to prevent the actual submit
return false;
}
Then in your html:
<form action = "/posts/search" onsubmit="return doSearch(this)">
<input type="text" name="searchstring" />
<input type="submit" />
</form>
How about adding the form this way:
<form method="get" action="<%= search_posts_url_path %>">#text input here with name "searchstring"</form>

rails custom html elements form

I am new in Rails and i've searched around the web and books but I couldnt find how to make custom forms when working with rails..
what happens is that I am used to write my own HTML tags and I confortable with that. I dont like to use libs like JSF (from JAVA) that writes html components for me and I dont want that rails write it for me, except for simple tags like
text_field(:post, :title, :size => 20)
# => <input type="text" id="post_title" name="post[title]" size="20" value="#{#post.title}" />
so.. how can I do that.. for example: I would like to write by myself
<input type="text" class="myclass" data="mydata" name="how-to-get-the-attribute-name-with-rails" value="how-to-get-value-with-rails" />
how can I do that?
If you want more control over the html you are creating, you can also use a content_tag
content_tag :input, "label_name", class: "myclass", data: "mydata", name: "how-to-get-the-attribute-name-with-rails", value: "how-to-get-value-with-rails"
You can supplement any html element tag for :input. So if you want a div instead, use :div etc...
As you have written above, The name of any form field in rails is in the following format
name = 'post[name]' i.e. model_name[attribute_name]
So your params hash will contain :post => {:name => value} which allows you to use mass-assignment. But if you want to get some extra parameters from the form you can directly include them in the form and give them any name as you want. It will be available in your params hash.
You can get value easily using value = <%= #object.attribute_name. %>
I am not sure if you wanted to know this or something else. Let me know if you need more help.
You can simply write html inside a Rails form, so
<input type="text" class="myclass" data="mydata" name="how-to-get-the-attribute-name-with-rails" value="how-to-get-value-with-rails" />
is perfectly valid, but note that you should use the names and id's in correct way to automatically bind them to your back end controller.
After all, what text_field(:post, :title, :size => 20) does is it convert the parameters to pure HTML (we call them helper methods in Rails).
Read here for formhelper options (using helpers will keep your code clean).