Pass variable between views (erb) using sinatra - sinatra

How can I pass a variable from one view to a partial view in Sinatra?
I already tried this:
view1.erb
<%= erb_partial :view2, locals: {test: "hello"} %>
view2.erb
<%= puts params[:test] %>
and also I tried:
view1.erb
<%= erb_partial :view2, test: "hello" %>
There are info. but for rails and for sinatra for sending one variable from controller to view, like this, but It did not help me unfortunately.
Any idea?

The local variable is passed correctly to the partial view. So, in the partial view, it will be available as a normal local variable -- test. You should not use params to access them, the data it contains are different from the local variables available in a view.
So you should use it just like how you use a normal local variable :
view2.erb
<%= test %>

Related

sails blueprint view iterates over hidden properties of empty object

I am new to sails, and trying to output a basic list coming from the find action from the blueprints.
Using an example view from the docs:
<ul>
<% _.each(data, function (project) { %>
<li><%= project.name %></li>
<% }) %>
</ul>
and checking the value of matchingRecords in the node-inspector returns [] (which i expected).
The view however lists 2 items with value undefined, see image below:
If I add data into the model, it iterates over the json returned by the blueprint char by char. I must be doing something wrong, but I am kind of stumped. I originally used Jade, and thought maybe there is something wonky with the template adapter, but as I said, ejs gives me a very similar result.
What I am doing wrong ?
It seems there is a bug in sails at the moment of writing.
An issue has been opened on Github https://github.com/balderdashy/sails/issues/3932

Perl - When <%method PREPARE> gets called

I am new to Perl Mason.
I came across this with the suggestion that service calls should be put inside PREPARE block. But when I placed my service calls inside it, seems like the code inside it is never getting executed itself.
<%method PREPARE>
Kindly suggest what is the above block for and its usage.
From the Mason Manual:
The base component class, Mason::Component, has but a few built-in
methods: handle, render, wrap, main, m, and cmeta.
The main method contains the mix of HTML and Perl in the main part of
the component.
You can add other methods that output HTML via the section;
these methods automatically have access to $self and $m.
<%method leftcol>
<table><tr>
<td><% $foo %></td>
...
</tr></table>
</%method>
...
<% # call leftcol method and insert HTML here %>
<% $.leftcol %>
Which means you are declaring a method named PREPARE without any argument lists by <%method PREPARE> and after writing the method body you will end it using </%method>.
And, later somewhere you will call it using <% $.PREPARE %>.For more info refer the Mason Manual.

Multiple form inputs for the same field

I have a Rails model called request_form. In the form I have multiple boxes that can be selected, similar to the "What can we craft for you section" in the following site http://mostlyserious.io/get-started/
I have a field called number_of_guests that can be a handful of values depending on which section is clicked. I plan on using javascript to grab the value of the clicked element and assigning it to the request_no_of_guest param. However, in the form I have that same field rendered 5 times to allow for each selection. What is the best way about this? Is there a better way to handle this? I thought about creating a controller method that loops through each instance of that request_no_of_guests param and determining which instance is populated and then sending that up.
Is this the best way to handle this case?
Well as you did not provide any useful detail I will answer as I understood the question.
Let’s have a quick look at what this might look like. To do so, we’ll imagine a demo app into which you can enter the name of a professor and select their various areas of expertise.
Professor has a field expertise which is String.
The form can be as follows:
<%= label_tag 'expertise_physics', 'Physics' %>
<%= check_box_tag 'professor[expertise][]', 'Physics', checked('Physics'), id: 'expertise_physics' %>
<%= label_tag 'expertise_maths', 'Maths' %>
<%= check_box_tag 'professor[expertise][]', 'Maths', checked('Maths'), id: 'expertise_maths' %>
alter app/controllers/professors_controller.rb
from
params.require(:professor).permit(:name, :expertise)
to
params.require(:professor).permit(:name, expertise:[])
Then in app/models/professor.rb
before_save do
self.expertise.gsub!(/[\[\]\"]/, "") if attribute_present?("expertise")
end
and in /app/helpers/professors_helper.rb
def checked(area)
#professor.expertise.nil? ? false : #professor.expertise.match(area)
end
This way you can grab the different values selected in the form into the expertise attribute, however this is not recommended if you are planning to query the selected options. The right way would be create a new model called Expertise and then create the relationship professor has_many expertises.
But if you are looking for something simple, then use the above code.

Silverstripe 3.0 customize tools panel

I want to show some customized content in the left panel which usually contains the treeview.
As the stuff in this panel will be an editable Gridfield wich should be related to the EditForm, I tried to built a new EditFormTools panel in this way:
I copied the CMSMain_Content.ss in mysite/templates/Includes and changed $Tools into $EditFormTools
I created the file CMSMain_EditFormTools.ss in the same directory with this code:
<div class="cms-content-tools west cms-panel" data-expandOnClick="true" data-layout-type="border" id="cms-content-tools-CMSMain">
<div class="cms-panel-content west">
<% include Test %>
</div>
</div>
I created a Test.php with:
class Test extends CMSMain{
public $var = 'test';
public function testfunction(){
$variable = 'hakuna matata';
return $variable;
}
}
Then I created a Test.ss with this code:
some Text
$var
$testfunction
$variable
The Panel appears in my CMS now but it only contains "some Text". So the include of Test.ss works perfectly fine but passing variables from Test.php to Test.ss doesn't.
Can anybody help?
Greetings
It may not directly answer your question but may give you direction.
You need to extend a controller class.
Then you can use a called functions to tell controller which template file it should use using renderWith().
for example,
public function index(){
return $this->renderWith("Test");
}
Then your function references in Test.ss will call functions in Test.php given it is the controller.
If Test class is not the controller rendering the template, the template doesnt know where your variable returning function is.
By the way, you can pass variables from layout to include template.

Asp MVC 2: Typed Editor Template

(I reference this tutorial in this text)
I want to use the Html.EditorFor (or Html.Editor) helpers.
If a UserControl needs additional data it is passed via
...EditorFor(model => model.Album, new { Artists = Model.Artists, ... })
In the UserControl it's accessed via ViewData[stringKey], ie
... new SelectList(ViewData["Artists"] as IEnumerable, ...
To me this smells a little fishy as I would prefer a strongly typed ViewModel which ensures that specific data is available.
I'm now a little bit stuck as I don't know wheater there's a "typed way" to find or I should accept this way as-is.
How did you solve this issue? Any help appreciated!
Lg
warappa
I would probably change my view model so that I don't need to pass this additional information. You could make for example an album has a collection of artists. Now all tha you will have to do is:
<%: Html.EditorFor(model => model.Album) %>
And in your editor template:
<%: Html.DropDownListFor(x => x.SelectedArtist, new SelectList(Model.Artists)) %>