I am trying to conditionally route two different controller actions. I have created RoutesController#root and sent root there from routes.rb, however the app just wants to find a root template to render no matter what i write in the root method.
What I am trying to achieve is:
user requests ‘/’
user gets pushed to sign in
upon successful sign in if current_user.company.present? (current_user should be available in the Routes controller right?) then render Quotes#new
else if no company then render Companies#new
I'm hitting a missing template error;
Missing template companies/1/quotes/new.1 with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "app/views"
I want it to be searching in app/views/quotes/new, what am i doing wrong?
RoutesController.rb
class RoutesController < ActionController::Base
before_filter :authenticate_user!
def root
if current_user.company.present?
render new_company_quote_path(current_user)# 'quotes#new'
else
render new_company_path(current_user) # 'companies#new'
end
end
end
routes.rb
root 'routes#root'
render is used when you just want to render/display a particular view from the path, it doesn't execute any code. For detailed differences, see this post.
Thus, in your case, it should be redirect_to instead of render.
And regarding the best practice thing, it looks good to me.
Related
I randomly get this error when in production from certain IP Addresses on the root path. The rails app does not support the formats :formats=>[:gif, "image/x-xbitmap", :jpeg, "image/pjpeg", "application/x-shockwave-flash", "application/vnd.ms-excel", "application/vnd.ms-powerpoint", "application/msword"] so this error seems to be expected if a request is being made for them. I'm guessing that someone or some bot is trying to run an exploit against the site -- how can I redirect or route these kinds of requests back to the route path such that an error is not produced?
ActionView::MissingTemplate: Missing template front_page/index, application/index with {:locale=>[:en], :formats=>[:gif, "image/x-xbitmap", :jpeg, "image/pjpeg", "application/x-shockwave-flash", "application/vnd.ms-excel", "application/vnd.ms-powerpoint", "application/msword"], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :arb, :jbuilder]}. Searched in:
* "/app/app/views"
* "/app/vendor/bundle/ruby/2.0.0/gems/activeadmin-1.0.0.pre2/app/views"
* "/app/vendor/bundle/ruby/2.0.0/gems/kaminari-0.16.3/app/views"
* "/app/vendor/bundle/ruby/2.0.0/gems/devise-3.5.2/app/views"
File "/app/vendor/bundle/ruby/2.0.0/gems/actionview-4.2.4/lib/action_view/path_set.rb", line 46, in find
File "/app/vendor/bundle/ruby/2.0.0/gems/actionview-4.2.4/lib/action_view/lookup_context.rb", line 121, in find
File "/app/vendor/bundle/ruby/2.0.0/gems/actionview-4.2.4/lib/action_view/renderer/abstract_renderer.rb", line 18, in find_template
The full error is here
To achieve the result desired in your comment:
constraints :format => "html" do
resources ...
end
Or if you need more flexibility:
# application_controller.rb
ApplicationController < ActionController::Base
before_action :check_format!
...
def check_format!
unless request.format == :html
render :nothing status: :bad_request
end
end
...
end
But overall, I feel like this is all a ton of overkill...
Plus, it's very typical to see buckets of respond_to in controllers, because the normal behaviour is to try and serve up the any format. Otherwise, there would probably be tons of configurations and such.
Is there a way to do this for all controller actions unless explicitly stated
So when you say unless explicitly stated you're sort of swimming against the current.
I'm not sure that this will work for you, but perhaps something like:
def index
...
respond_to do |format|
format.html
format.all { render status: :bad_request }
end
end
I have Active Model Serializer setup and I have the create action to render show on successful creation of the record. If I go directly to a record by URL (/xyz/1) then it renders just fine using AMS. However when using the render method I get the following error:
ctionView::MissingTemplate at /contracts.json
==============================================
> Missing template v1/contracts/show, v1/base/show, base/show, application/show with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}. Searched in:
I have a feeling if I create a show.erb.json file then the message will go away but then Active Model serializer will not be used.
Here is the line with that is causing the error:
render :show, status: :created, location: get_resource
If the get_resource method is needed please let me know.
I have a rails app running on passenger with a mailer. I have it so the page following the action shows the email content but I get this error:
Missing template rfis/mail, application/mail with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :slim, :jbuilder]}. Searched in: * "/var/www/rqm3/app/views" * "/usr/local/rvm/gems/ruby-2.1.5/gems/devise-3.5.1/app/views" * "/usr/local/rvm/gems/ruby-2.1.5/gems/twitter-bootstrap-rails-3.2.0/app/views"
despite this, the email does correctly load the template when being sent. It's only showing it in a view that causes issues.
rfis_controller.rb:
def mail
#mail_message = RfiMailer.send_rfi(current_user, #rfi).deliver
end
rfi_mailer.rb:
class RfiMailer < ApplicationMailer
default from:"testemail#mail.com"
def send_rfi(user, rfi)
mail(to:"other#test.com")
end
end
send_rfi.html.erb:
test
Issue is due to you are not redirecting anything inside your method "mail" define inside controller
def mail
#mail_message = RfiMailer.send_rfi(current_user, #rfi).deliver
redirect_to #user, notice: "Message sent successfully ."
end
you can also define render nothing
I'm a newbie to Rails and beginning my crud application. I've created the form perfectly fine but when I click the submit button this error appears, could someone please explain the error and how I would go about resolving this error
Missing template posts/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/Users/shannon/beginner/app/views"
My controller:
class PostsController < ApplicationController
def new
end
def create
render plain: params[:posts].inspect
end
May be you are using Rails version prior 4.1, render plain is added in Rails 4.1. So Rails is ignoring plain option and looking for template posts/create.
in Rails 4.1 you can do:
render plain: params[:posts].inspect
in Rails 4.0 you need to do:
render text: params[:posts].inspect
I figured I should finally write some tests for my rails app.
My controller is "UsersController". It doesn't have any HTML as I just have an iphone app sending a post in to a rails controller.
Here is my test:
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def test_create
# to http post
# /users
#user[email]=%#&user[password]=%#&user[password_confirmation]=%#
#post
post(:create, :user => {:password => "testpassword", :password_confirmation => "testpassword"})
end
Problem is that I get this error:
1) Error:
test_create(UsersControllerTest):
ActionView::MissingTemplate: Missing template users/new with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths
So I guess it's trying to populate an HTML page? If so, I find this odd. I would think it would directly do the post to the controller. Can someone confirm that this "post" method tries and populates an HTML form?
If this is the case, how should I proceed in writing a test to directly send an HTTP post to the controller?
Thanks for any help
You can specify "format" to make it work:
post(:action, {'param1'=>'value1', 'param2' => 'value2', :format => 'js'})
Unless you tell it otherwise the post method assumes the requested content type is HTML. Typically the create action looks something like this:
#user = User.new(params[:user])
if #user.save
redirect_to posts_path
else
render :new
end
If the action fails it tries to render 'users/new', which doesn't exist, thus the error.
So there are a couple of issues here. It's not clear what content type is expected (XML?). The action is failing, but we can't see why. It might help to edit the question to show us what the controller action does.