How to access a page's URL in VoltRb - ruby

I'm trying to run some code on a controller in Volt, but only on certain pages. I've looked through the docs, but I'm not really sure how to get access to a given page's URL. Is there perhaps some hidden variable or something in the page model, like so?:
module Main
class MyController < Volt::ModelController
model :page
def index
end
def template_page
if page.url == "/foo/bar" # obviously, this doesn't actually work
# run some code
end
end
end
end

If you are using bindings in your URL, for example
client '/examples/{{ category }}/{{ example }}', controller: 'examples', action: 'template'
you can access those in the controller via the params collection:
params._category
params._example
In other cases you URL should be static anyway.

Sorry for the late reply. I added docs for the url method which is available from controllers:
http://docs.voltframework.com/en/docs/url.html

Related

Recieve url params with #api_view FBV

Is it possible to pass parameters from URL to a view function decorated with #api_view or I need to use APIView class instead?
Yes it's possible. What you've to do is access the request.query_params as below,
#api_view()
def sample_view(request, kw, *args,**kwargs):
url_params = request.query_params
# ypur code

Phoenix not loading View module

I have a SessionController with a corresponding SessionView and within the templates directory I have a "session" folder with a "new.html.eex" file. When I navigate to the session_path "new" action I recieve an error:
HelloWeb.SessionView is not available
It looks like it is calling SessionView.render/2
In the session_controller I'm simply trying to render the new.html.eex, here's the new action:
def new(conn, _params) do
render conn, "new.html"
end
Phoenix should be rendering the "new" template but the error keeps coming up and I'm not sure why. Everything is spelled correctly and I have the routes correctly mapped in the "router.ex" file.
## Routes for sessions ##
get "/login", SessionController, :new
post "/login", SessionController, :create
delete "/logout", SessionController, :delete
However, a call to "login" yields the error
SessionView.render/2 is undefined (module HelloWeb.SessionView is not available).
What is going on that's causing Phoenix to not load the "new" template?
Update: Here is the session_view:
defmodule Gofish.SessionView do
use GofishWeb, :view
end
You need to create a session_view.ex on the lib/hello_web/views directory.
Its minimal content will be:
defmodule HelloWeb.SessionView do
use HelloWeb, :view
end

How to write specific custom routes for rails + devise?

I am trying to write a custom route that will point to a custom controller action in devise.
I have the setup below right now.
# custom controller
class Registrations::RegistrationsController < Devise::RegistrationsController
layout 'settings'
# GET /resource/edit
def edit
super
end
end
# routing setup
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations/registrations" },
path_names: { edit: 'profile' }
end
This allows me to have a custom URL localhost:4000/users/profile with no problems.
My question is how can I customize this further to be
localhost:4000/profile
localhost:4000/settings/profile
Note I know that I can set path: '' or path: 'settings', but that will affect all routes within users.
Is there a way that I could have
localhost:4000/settings/profile and localhost:4000/login at the same time using devise_for?
I am not sure how to control these affects separately.
As we can see here, we can use Rails scopes and specify a controller for 'registration', for example. Something like this:
scope :settings do
devise_scope :user do
get '/profile' => 'devise/registrations#edit'
end
end

Laravel - Request::server('HTTP_HOST') returns 'localhost' from within a helper class

I want to get the current domain, using Request::server('HTTP_HOST') - however when I call this from within a helper class it comes back as 'localhost' which is not what I want. From a controller it works as expected. Is there a way to access this information from within a helper class?
The helper class looks like this:
class ApiWrapper {
public static function call($model, $method='', array $input) {
$domain = Request::server('HTTP_HOST');
}
}
You can do URL::to('/') to get the base URL of the Laravel application, if that's what you're asking for. If doing Request::server('HTTP_HOST') from your controller is giving you the desired result, doing the same from the helper class shouldn't be any different.

Controlling Merb authentication errors

Hey there, im a little bit confused about handling invalid user authentication request, at login controller. So, i already have modified login view, but cant figure out where to put the exception handling block. It should work like this: you login - if its incorrect you will see warning message at /login .
Any ideas ?
What strategy have you chosen ? In my custom Strategy, I call the class method 'authenticate' on my User class:
class User
def self.authenticate(login, password)
u = User.first(:conditions => ['email = ?', login]) # find a user with this login
if u && u.authenticated?
return u
else
nil
end
end
end
Also, you might want to look at the source code of merb-auth-more/mixins/salted_user which is a module that is automatically mixed into your User class.
you would put your exception handling action in the exceptions controller
# handle NotAuthorized exceptions (403)
def not_authorized
render :format => :html
end
and to customise the view you would create a template in app/views/exceptions/not_authorized.html.haml

Resources