Embedding variable inside text using Trix - ruby-on-rails

I would like to let users use variable name inside text box with Trix.
Given a trix text-box that let the user write messages to other user, I would like to be able to do something like this:
"Hello %user_name%"
When sending the message the user_name would be changed to whatever is the user_name.
I would except this to be a fairly standard feature but couldn't find mention of it in the documentation or SO.
I have tried doing a simple .gsub on the model.content.body but this is returning and ActionText::RichText who doesn't know what gsub is.

An easy way to mock variables within text contents is to use search and replace, just replace the strings within the action that receives posted contents:
final_message = params[:message].gsub("%user_name%", "John Doe")
Note that gsub is only available for string type.

Related

How can I put a link inside of a text string in HAML?

This should probably be easier than it is. I just want to put a link inside an HTML paragraph element.
%p{class: "answer"}="Please upload your data to this portal in text (tab-separated) format. Download our template #{raw(link_to 'here', '/templates/upload_template.xlsx')} for sample data and a description of each column."
Rails is encoding the tag information. I don't want tags to be encoded. I want them to be tags.
You can use more than one line inside any block, to solve your problem we will have something like this:
%p{class: "answer"}
Please upload your data to this portal in text (tab-separated) format. Download our template
= link_to 'here', '/templates/upload_template.xlsx'
for sample data and a description of each column."
You can use interpolation directly in Haml, and doing this seems to fix the issue in this case.
So instead of doing this:
%p= "Text with #{something_interpolated} in it."
you can just do
%p Text with #{something_interpolated} in it.
i.e. you don’t need the = or the quotes, since you are just adding a single string. You shouldn’t need to use raw either.
(Also, note you can do %p.answer to set the class attribute, which may be cleaner if the value isn’t being set dynamically.)
Why this is being escaped here is a different matter. I would have expected the two cases (%p= "#{foo}" and %p #{foo}) to behave the same way. However, after a bit of research, this behaviour seems to match how Rails behaves with Erb.

How can I take this text and store it in a variable?

I'm not sure if I can accomplish this with Lua, but I want to take the data from the following URL:
http://www.roblox.com/Trade/InventoryHandler.ashx?token=%22&filter=0&userid=1210210&page=1&itemsPerPage=14&_=
and store it in a variable so I can manipulate it.
Is there any way I can do this?
To save a string as a variable it is as simple as
foo = "contents of string"
To manipulate the string, you would use the string library
If you want the text at the URL instead of in the url, see the link to the previously asked question I added in the comments

how to display interactive code in rails view?

The code itself would be in ruby. Idea is to make it interactive for user. For instance, code asks:
what is your name?
user input "John"
Hi John!
I know I can make it <%...%>. I want to make it in a separate ruby.rb file that would be "uploaded" via form in new template. In show template results of code would be displayed. Any gem for this sort of interactivity?
Looking forward
Thanks
This is incredibly bad practice, and should never be used without sanitizing input, but Ruby has an eval statement. Pass it a string (such as a param POSTed by your form), and it'll evaluate that string as Ruby and return the result.
x = 5
eval "x / 2.5"
=> 2.0
If you're expecting a .rb file to be uploaded, you can read that file and pass the contents to eval.
But remember, treat all input as hostile.

Grails: User inputs formatted string, but formatting not preserved

I am just starting a very basic program in Grails (never used it before, but it seems to be very useful).
What I have so far is:
in X.groovy,
a String named parameters, with constraint of maximum length 50000 and a couple other strings and dates, etc.
in XController.groovy,
static scaffold = X;
It displays the scaffold UI (very handy!), and I can add parameter strings and the other objects associated with it.
My problem is that the parameters string is a long string with formatting that is pasted in by the user. When it is displayed on the browser, however, it does not retain any carriage returns.
What is the best way to go about this? I'm a very beginner at Grails and still have lots and lots of learning to do on this account. Thanks.
The problem is that the string is being displayed using HTML which doesn't parse \n into a new line by default. You need to wrap the text in <pre> (see: http://www.w3schools.com/tags/tag_pre.asp) or replace the \n with <br/> tags to display it correctly to the user.

Parse and output TypoScript in TypoScript

I have extended the pages table with one text field named TypoScript.
My intention was to use this field in TMENUs so that I can display arbitrary content as — for example — a dropdown for that page.
I know how to parse and output TypoScript in an extension, but I'm not sure if there's a way to do that in TypoScript alone.
So, in summary: Is it possible to have a TypoScript string and parse and output it in TypoScript?
You have to use a user function for that.
To be more specific, you have to use either the USER or the USER_INT content object.

Resources