I am getting this error:
StaticController#home is missing a template for request formats: text/html
This is my controller:
class StaticController < ApplicationController
def home
# render json: {status: "is working"}
end
end
When I uncomment the comment above, it renders that.
my views/static/home.html.erb view:
<%= javascript_pack_tag 'index' %>
and my routes:
Rails.application.routes.draw do
resources :sessions, only: [:create]
resources :signup, only: [:create]
delete :logout, to: 'sessions#logout'
get :logged_in, to: 'sessions#logged_in'
root to: "static#home"
# get '*path', to: "static#home", via: :all
end
Related
I'm sure this should be a simple issue, but can't find a solution (im new to ROR).
This is the error im getting:
-----error------
NoMethodError in Agency::Clientmanagement#edit
Showing /home/ubuntu/environment/nacho/app/views/agency/clientmanagement/_form_client.html.erb where line #2 raised:
undefined method `client_path' for #ActionView::Base:0x007fcda8bd7ae0
Did you mean? clients_path
<%= form_with model: #clientrecord do |f| %>
------ This is my routes------
root 'pages#home'
get 'clients', to: 'clients#index'
get 'agency', to: 'agency#index'
get 'admin', to: 'admin#index'
get 'clientview', to: 'agency#clientview'
#post 'clientview', to: 'agency#clientview'
namespace :admin do
get "new", to: 'manager#new'
get "add-user", to: 'manager#add-user'
get "agencies", to: 'manager#agencies'
end
namespace :agency do
resources :clientmanagement, only: [:show,:new, :create, :edit]
resources :team, only: [:index, :show,:new, :create, :edit]
end
-------this is the: admin/clientmanagementcontroller
class Agency::ClientmanagementController < ApplicationController
before_action :require_agencyaccount
before_action :user_clients
before_action :set_client, only: [:show]
# before_action :set_one_product, only: [:show]
# def index
# #all_shoe_products = #all_products.where(main_category_id: MainCategory.find_by_name("shoes").id)
# end
def show
end
def new
end
def create
end
def edit
#clientrecord = Client.find(params[:id])
end
private
def user_clients
#clients = User.find(current_user.id).clients
end
def set_client
#client = Client.find(params[:id])
end
--- here is the form_with in the form partial being rendered in /agency/clientmanagement/{id}/edit
<%= form_with model: #clientrecord do |f| %>
Any help would be greatly appreciated
In your routes.rb file I don't see client_path is defined.
Option 1: You can add client path by adding the following route below clients path:
Rails.application.routes.draw do
get 'clients', to: 'clients#index'
get 'client', to: 'clients#show'
...
end
this will generate the following path:
client_path GET /client(.:format) clients#show
Option 2: If you have a full resource controller for clients, you can add
resources :clients
this will generate all RESTful routes for clients
clients_path GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client_path GET /clients/new(.:format) clients#new
edit_client_path GET /clients/:id/edit(.:format) clients#edit
client_path GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy
I have one controller for creating category, and I want to have one more nested controller to create language versions of categories. I want to avoid methods like new_language, edit_language, ... and routing on them, I would like to do it the best rails way. But I am new to rails (from Padrino and Sinatra), and I am little bit lost in routing.
I have my controller for categories
module Admin
class CategoriesController < ApplicationController
before_action :authenticate_user!
before_action :find_category, only: [:show, :edit, :update]
layout 'admin'
def index
#categories = Category.all
end
..... all the others CRUD methods ....
end
end
And another controller.
module Admin
class CategoriesLanguageController < ApplicationController
before_action :authenticate_user!
before_action :find_category, only: [:show, :edit, :update]
def new
#category = Category.find(params[:category].to_i)
end
.... all the others CRUD methods ...
end
end
and my routes config.
Rails.application.routes.draw do
devise_for :users
resources :users
namespace :admin do
get '/' => 'dashboard#index'
resources :dashboard, only: [:index]
resources :categories do
collection do
get :publish_category
#get :new_lang
#post :create_lang
#get :edit
#post :update
#get :destroy
end
resources :language, only: [:new, :create, :edit]
end
end
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root to: "homepage#index"
namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :categories, only: [:index]
end
end
end
I would like to have methods new_lang and create_lang under second controller with default names new, create, ....
It generates this
admin GET /admin(.:format) admin/dashboard#index
admin_dashboard_index GET /admin/dashboard(.:format) admin/dashboard#index
publish_category_admin_categories GET /admin/categories/publish_category(.:format) admin/categories#publish_category
admin_category_language_index POST /admin/categories/:category_id/language(.:format) admin/language#create
new_admin_category_language GET /admin/categories/:category_id/language/new(.:format) admin/language#new
edit_admin_category_language GET /admin/categories/:category_id/language/:id/edit(.:format) admin/language#edit
admin_categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_admin_category GET /admin/categories/new(.:format) admin/categories#new
edit_admin_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
admin_category GET /admin/categories/:id(.:format) admin/categories#show
PATCH /admin/categories/:id(.:format) admin/categories#update
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
but it is not working.
ActionController::UrlGenerationError in Admin::Categories#index
and error is this route:
<td><%= link_to cat.internal_name, edit_admin_category_language_path(id: cat) %></td>
And I don't know how to create these actions in another controller and use url helpers.
Or this is not the best way to do it?
Please any advice?
I'm trying to export a docx file using caracal but I'm getting a routing error, but everything seems to be okay.
I did this 3 days ago exactly like now and worked, now I'm getting an error.
Routes.rb
Rails.application.routes.draw do
get 'grayscale/index'
devise_for :users, path: '', path_names: {sign_in: 'login', sign_out: 'logout', sign_up: 'registrar'}
resources :contratos
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'contratos#index'
get 'contratos/page'
end
contratos_controller.rb
class ContratosController < ApplicationController
before_action :authenticate_user!
before_action :set_contrato, only: [:show, :edit, :update, :destroy, :export, :page]
access all: [:show, :index], user: {except: [:destroy, :new, :create, :update, :edit]}, site_admin: :all
require './lib/generate_pdf'
# GET /contratos
# GET /contratos.json
def index
#contratos = Contrato.all
end
# GET /contratos/1
# GET /contratos/1.json
def show
end
# GET /contratos/new
def new
#contrato = Contrato.new
end
# GET /contratos/1/edit
def edit
end
def page
Caracal::Document.save(Rails.root.join("public", "example.docx")) do |docx|
# page 1
docx.h1 'Page 1 Header'
docx.hr
docx.p
docx.h2 'Section 1'
docx.p 'Lorem ipsum dolor....'
docx.p
end
path = File.join(Rails.root, "public")
send_file(File.join(path, "example.docx")
end
show.html.erb
<%= link_to 'Generate Docx', contratos_page_path %>
The full error
ActiveRecord::RecordNotFound in ContratosController#show
Couldn't find Contrato with 'id'=page
def set_contrato
#contrato = Contrato.find(params[:id])
end
This is a very common beginner issue which is due to the fact that routes have precedence in the order they are declared (thus the comment on top of routes.rb).
Since resources :contratos already defines a GET /contratos/:id route it will always match the request for GET /contratos/page to contratos#show. Rails does not assume that your ids are numerical when routing. These paths will all match the GET /contratos/:id route:
GET /contratos/1
GET /contratos/page
GET /contratos/page?foo=bar
GET /contratos/foo-bar-baz
GET /contratos/alksjd-usfiugi%-dfgd
But these will not:
GET /contratos/new # declared before the show route
GET /contratos/1/foo
GET /contratos/foo/bar
You can fix this by moving your custom route to the top:
get 'contratos/page'
resources :contratos
But there is a better Rails way of adding additional restful actions to a resource:
resources :contratos do
get :page, on: :collection
end
http://localhost:3000/users.json?q=lala
I want to get json by sending some string,
But I can not get the expected string 'lala' in params[:q]
p(params) `{"action"=>"index", "controller"=>"users", "locale"=>"en"}`
controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
# #users = User.all
#users = User.order(:name)
(1..100).each {p(params)}
respond_to do |format|
format.html
format.json {render json: #users.where("name like ?", "%#{params[:q]}%")}
end
end
This is my route.rb
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
resources :articles
resources :users
root to: 'articles#index'
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }, :via => [:get]
match '', to: redirect("/#{I18n.default_locale}"), :via => [:get]
match '', to: redirect("/#{I18n.default_locale}"), :via => [:get]
if you put resources :users outside the scope ":locale",then the routes will generate normal routes like
GET /users users#index users_path
RESTful routing creates by default.
Please see this
I am writing an application , which User can create Topics and others can make posts on that topic.
I am stuck with this error :
No route matches {:action=>"show", :controller=>"topics", :id=>nil}
my route.rb :
MyPedia2::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :topics, only: [:show, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/topics/:id', to: 'topics#show'
my rake route shows :
topics POST /topics(.:format) topics#create
topic GET /topics/:id(.:format) topics#show
DELETE /topics/:id(.:format) topics#destroy
root / static_pages#home
/topics/:id(.:format) topics#show
and my topics controller is:
# encoding: utf-8
class TopicsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def show
#topic = Topic.find_by_id(params[:id])
end
def create
#topic = current_user.topics.build(params[:topic])
if #topic.save
flash[:success] = "Konu oluşturuldu!"
redirect_to root_path
else
render 'static_pages/home'
end
end
def destroy
#topic.destroy
redirect_to root_path
end
private
def correct_user
#topic = current_user.topics.find_by_id(params[:id])
redirect_to root_path if #topic.nil?
end
end
Is there a fix for this ?
EDIT : I found that _topics.html.erb fails
I found what breakes the code :
<% for topic in #topics do %>
<li><%=link_to topic.title, topic_path(#topic) %></li>
<%= will_paginate #topics %>
<% end %>
topic_path(#topic] part is wrong. How can i make it to use id?
It's not working because your collection is '#topics', and each element is 'topic', not '#topic'. But you're close. Try this:
<li><%=link_to topic.title, topic_path(topic) %></li>
Try this:
<li><%=link_to topic.title, topic_path(:id => #topic.id) %></li>
I think your routes should probably read:
resources :sessions, :only => [:new, :create, :destroy]
resources :topics, :only => [:show, :create, :destroy]
After hours of thinking , now i can see my mistake. I used show method in my topics controller but i did not have show.html.erb in my views/topics.
If you want to show your topics you must use these methods:
1) in config/routes.rb use :
match '/topics/:id', to: 'topics#show'
2) in the model i used
belongs_to :user,:foreign_key => "user_id"
3) link as :
<li><%=link_to topic.title, **topic_path(topic)** %></li>
4) and prepare the template you mentioned in the route.
I hope this help anyone.