Can't put my Error template outside the error folder - phoenix-framework

I created a standard not_found page that is working OK as long as I have it inside the "web/templates/error" folder (not_found.html.eex).
I'm using (in error_view.ex):
def render("404.html", _assigns) do
render("not_found.html", %{})
end
When I try to move it into another folder (in this particular case the standard "web/templates/page" folder) I get the "Server internal error" message that is suppose to appear because no render clause matches or no template is found. The template is there and I've tried with different folders.
I'm using this render function in error_view.ex:
def render("404.html", _assigns) do
render("MyProject.PageView", "not_found.html", %{})
end
Shouldn't this work?

The render/3 function takes 3 arguments. The first argument should be the module to call render on:
def render("404.html", _assigns) do
render(MyProject.PageView, "not_found.html", %{})
end
You have provided the string "MyProject.PageView" instead of the module MyProject.PageView.

Related

get text with index number and then compare with the expected text

I need to write a method which will get text with the help of index number from popup and then i need to compare with the expected text
i.e i need to verify expected plan name is displayed at the bottom of the popup box
Setting the correct id for the query (which you can get by doing on calabash console the command query("*", :id)) on code below should do the trick. If you can't use id try to get another component property (like Android component by using query("*") ) and set the query inside theget_text calls.
def get_text(query)
query(plan_query, :text).first
end
def text_equals(text, expected_text)
unless text == expected_text
fail "#{text} not equal to #{expected_text}"
end
end
def verify_plan(index, expected_text)
plan_text = get_text("* id:'PLAN_TEXTS_ID' index:#{index}") # Can change 'id:...'' by Android class if plan does not have id
expected_text = get_text("* id:'BOTTOM_PLAN_ID'") # Same as above
text_equals(plan_text, expected_text)
end

Best practice for having the result of a same query in all routes

I would like to be able to pass data for a destak popup I have that can be shown on every route/page of my application. The data is on the database and thus I use a query to get it and pass it in a controller to a specific page:
def admin(conn, _params, locale) do
destaks = Data.listAll(query)
render(conn, "admin.html", destaks: destaks)
end
What's the best way of have it available for all pages, while making sure that when I update that data in the database it is reflected automatically on all pages?
You can use a custom Plug function for this. Here is an example:
1) Define your plug function somewhere (for my example I put this right into router.ex). The first argument is the connection that we will be adding our data to, and we don't need the second argument in this case:
def database_thing(conn, _) do
# This is where you get things from the database
data_from_a_query = ["these", "will", "be", "from", "your", "query"]
Plug.Conn.assign(conn, :values_from_database, data_from_a_query)
end
2) Add plug function to a pipeline in router.ex, you can add it to an existing pipeline or create a new one:
# Example of adding to an existing pipeline
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :database_thing # add it
end
# Example of creating a new pipeline
pipeline :everytime do
plug :database_thing
end
3) Make sure that your pipeline is in your scope. If you added it to an existing pipeline you shouldn't have to do much. If you created a new pipeline then you have to add it to the scope in router.ex:
scope "/", MyPhoenixApp do
pipe_through [:browser, :everytime]
resources "/users", UserController
end
4) Access the value in the controller. If you look at step one, you can see that we are assigning the data with the key :values_from_database. To access that data, you would do the following in your controller functions:
def index(conn, _params) do
IO.inspect(conn.assigns.values_from_database)
# ...
end
5) Access the value in the template. The conn is passed through the controller functions to the templates, so depending on what you are trying you may not need to access the value in the controller functions at all, and just use it directly in the template:
<%= #conn.assigns.values_from_database %>

Add default view for Rails Controller

I currently have no index views (app/views/mycontroller/index.html.erb), but would like to specify a default view if the user just types in: localhost:3000/mycontroller
Very new to RoR. My current config/routes.rb:
MyTestApp::Application.routes.draw do
get "team/thatguy"
get "home/about"
root :to => "home#about"
end
In my controller I've been playing around with redirects, but the desired solution is to only show the smaller uri localhost:3000/mycontroller.
app/controllers/home_controller.rb:
class HomeController < ApplicationController
def index
render "home/about"
end
def about
#title = "My Title"
end
end
Ok, assume you want to render a contact page when you access localhost:3000/mycontroller
What you want to do is:
Create a controller (C of MVC) with a contact action.
$ rails generate controller StaticPages contact
Excuting this code in your console creates some directories and files:
static_pages_contoller.rb with a contact action.
static_pages directory under the app/views directory with a view template called contact.html.erb
and you may have get 'static_pages/contact' in your config/routes.rb
Modify your routes:
Now you have access to the contact template with: localhost:3000/static_pages/contact
So to change the path, add a line to the routes.rb: match '/mycontroller', 'static_pages#contact', via: 'get'
Then you'll get your desired results.
I might be wrong, the whole process and details are here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Get the type of object that called a registered function in QTP

I'm trying to develop a generic registered function that verifies the existence of a given object, then adds a line to the report to say whether it was found or not.
Here's the function:
'#Description Reports in the test results whether the given object exists.
Public Function verifyExistence(ByRef test_object)
If test_object.Exist(1) Then
Reporter.ReportEvent micPass, "Verify that the page exists.", "Page exists."
Else
Reporter.ReportEvent micFail, "Verify that the page exists.", "Page does not exist."
End If
End Function
RegisterUserFunc "Page", "verifyExistence", "verifyExistence"
This works fine for Page objects, but how can I 'genericize' this so it can be used with any object type? I realize I'll need to add a RegisterUserFunc line for each object type.
Ideally, I'd have a line that looks like this:
typename = getType(test_object)
if typeName = "Page" Then
objName = test_object.GetROProperty("title")
... 'and so on.
end if
Try using test_object.GetTOProperty("micclass") for the test object's name.
Some dynamic objects (e.g. those returned from ChildObjects) may not have this property set on QTP's side in which case you should do test_object.GetROProperty("micclass").
The RO method should always work but its a bit slower than the TO method which should almost always work.
The TypeName() function should return (nearly) the type name of the object, e.g. "IRegExp2" for a RegExp.

ruby block questions loop variables

comics = load_comics( '/comics.txt' )
Popup.make do
h1 "Comics on the Web"
list do
comics.each do |name, url|
link name, url
end
end
end
I am new to ruby. This is a piece of code from a ruby website.
I cant find what 'link' and 'list' keyword in the menu.
can someone explain it a little bit those two keywords, and where is the definition of those two keyword .
I am also confused on how they read the variables name and url, they are reading it by the space at the same line or what?
so if I have
Comics1 link_of_comics_site_1
Comics2 link_of_comics_site_2
Comics3 link_of_comics_site_3
so for the first iteration, name=Comics1, and url =link_of_comics_site_1
Thanks.
That's not just Ruby. That's a template for a webpage using ruby add-on methods for HTML generation.
But presumably, the result of the call to load_comics is a Hash, where the keys are names and the values are URLs. You could make one of those yourself:
my_comics_hash = { "name1" => "url1", "name2" => "url2" }
which you can then iterate over the same way:
my_comics_hash.each do |name, url|
puts "Name #{name} goes with URL #{url}"
end
In your code, it's building up an HTML list inside a popup window, but it's the same idea. The each method iterates over a collection - in this case a Hash - and runs some code on every item in that collection - in this case, each key/value pair. When you call each, you pass it a block of code inside do ... end; that's the code that gets run on each item. The current item is passed to the code block, which declares a variable to hold it inside the pipes right after the word do. Since we're iterating over key/value pairs, we can declare two variables, and the key goes in the first and the value in the second.
In ruby function, parenthesis is optional and the ";" end of statement is also optional. ej
link "click here" , "http://myweb.com"
is equivalent to :
link("click here", "http://myweb.com");
But If you have more than one statement in a line the ";" is a must, ej
link("click here1", "http://myweb.com"); link("click here2", "http://myweb.com");
In your code it could be written in
link(name, url)
or just
link(name, url);
or
link name, url
But it is highly recommended to put parenthesis around function parameters for readability unless you have other reason . The ";" is not common in ruby world .

Resources