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
Related
Can't work out how to get around this. I'm using the mailboxer gem, who have recently updated to a namespaced model. Using the main controller, everything is fine, but I also have a namespaced views/controller for an admin section which is causing the issue.
Model name is
mailboxer_conversations
Main section code (working fine):
routes.rb
namespace :mailboxer, path: '', as: nil do
resources :conversations, only: [:index, :show, :new, :create, :destroy], as: 'conversations', path: 'conversations' do
member do
post :reply
post :trash
post :untrash
end
end
controllers/mailboxer/conversations_controller.rb
class Mailboxer::ConversationsController < ApplicationController
end
The above is all working fine, the controller is namespaced with folders and I can access the Conversation model instance.
The below is the code I'm currently trying for the admin section, using the same model mailboxer_conversations.rb
routes.rb
namespace :admin do
namespace :mailboxer, path: '', as: nil do
resources :conversations, as: 'conversations', path: 'conversations'
end
end
controllers/admin/mailboxer/conversations_controller.rb
class Admin::Mailboxer::ConversationsController < ApplicationController
end
With the above setup, I'm getting a nameerror uninit. constant 'Conversations'. Says to me that it can't access the model, is this because of the double namespace, it's expecting the model.rb file to be in a different folder i.e admin/mailboxer_conversations.rb? I can't move the model, as it's in a gem.
Thanks
Devise (2.1) was using my custom views fine until I told it to use a custom controller. Now it ignores my custom views.
Previously everything worked fine:
Tell Devise to use custom views in /config/devise.rb
# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
config.scoped_views = true
Add custom view: /app/views/subscribers/session/new.html.erb
Set up routes in /config/routes.rb
devise_for :subscribers
Then I added a custom SubscriberSessionsController as /app/controllers/subscriber_session_controller.rb
class SubscriberSessionsController < Devise::SessionsController
before_filter :isInIframe
private
def isInIframe
#hide_navbar = session[:in_iframe]
end
end
And modified /config/routes.rb to tell Devise to use this new controller instead of its default:
devise_for :subscribers, :controllers => {
:sessions => "subscriber_sessions"
}
Once I restart my server, Devise now uses this controller but ignores my custom view.
As is so often the case, ten minutes after posting the question I cracked it.
The reason Devise wasn't finding the view was it was looking for it in a different folder.My replacement controller was called subscriber_sessions.rbso devise was no longer looking in views/subscribers/sessions but views/subscribers/subscriber_sessions.
I solved this problem with the following:
Changed my subscriber routes to:
devise_for :subscribers, :controllers => {
:sessions => "subscribers/sessions"
}
Renamed my subscriber_sessions controller to just sessions and moved it into a subscribers folder so its new name & location are: app/controllers/subscribers/sessions_controller.rb
I also had to add a namespace to the class so the new sessions_controller.rb file looks like this"
class Subscribers::SessionsController < Devise::SessionsController
before_filter :isInIframe
private
def isInIframe
#hide_navbar = session[:in_iframe]
end
end
I'm attempting to upload an image via ajax using paperclip.
I'm using the qqfileuploader for the ajax stuff and it doesn't seem to have an option where I can define the parameter name for the post request.
The parameters sent from the ajax post are
qqfile=filename.jpg
so in my model, I have aliased qqfile to photo
alias_attribute :qqfile, :photo
has_attached_file :photo
attr_accessible :title, :photo
when I upload a file via ajax, I get the following errors
Parameters: {"qqfile"=>"Penguins.jpg"}
WARNING: Can't verify CSRF token authenticity
Creating scope :page. Overwriting existing method User.page.
User Load (1.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Creating scope :page. Overwriting existing method RoleUser.page.
Creating scope :page. Overwriting existing method Role.page.
Role Load (1.4ms) SELECT `roles`.* FROM `roles` INNER JOIN `role_users` ON `roles`.`id` = `role_users`.`role_id` WHERE `role_users`.`user_id` = 1
SQL (0.7ms) BEGIN
Creating scope :page. Overwriting existing method Task.page.
[paperclip] Duplicate URL for photo with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in Recipe class
I'm not sure if the CSRF token will be an issue, there is a token on the page, so maybe I just need to be sending that, I assume I can get it is a variable with javascript?
But what is the deal with the duplicate url??? Am I not aliasing correctly? Can I not alias a paperclip object for some reason?
my controller is also very simple
def create
#recipe = Recipe.new(params[:recipe])
#recipe.author_id=current_user.id
if #recipe.save
return render :json => #recipe
else
return render :text => 'an error occured saving the recipe'
end
end
Rails generates a security token for POST events based on the user's session. If that token is missing or doesn't match what's expected, the session will be reset. See this:
http://guides.rubyonrails.org/security.html#csrf-countermeasures
As for the duplicate URL, are you sure your URL pattern is specific enough? It looks to me that if you upload a file with the same name for the same model instance you'd have a problem. It would help to see your controller code.
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
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