Nested controllers Form_with Devise Error - html

So, I have a RailsApp and I decided to do a nested controller to my users called backoffice.
My routes to index are working fine, but when I try to edit or create a user, I get the following error:
NoMethodError in Backoffice::Users#new
undefined method `users_path' for #<#
<Class:0x007efc6d4fd1c8>:0x007efc6cd38708>
Did you mean? user_session_path
Here are my routes:
Rails.application.routes.draw do
resources :advertises
resources :categories
devise_for :users
namespace :backoffice do
resources :users, except: [:show]
end
end
I'm using Rails 5.1.4, so the 'form_tag' and 'form_for' are replaced for the 'form_with'
Here are my _form.html.erb where are my problem:
<%= form_with(model: user, root: true) do |form| %>
The structure of the controller is
class Backoffice::UsersController < BackofficeController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :pundit_user
....
Could the problem be with devise? Or it's just a nested controller error?

You should probably move the devise_for call into the :backoffice namespace as that is where the users resource is located.
If you run rails routes in the command line you will see the problem. Devise is trying to call users_path but that doesn't exist, what exists is backoffice_users_path

Related

Getting error (undefined method) for form_with

I have _form.html.erb. and on the first line of code, it is throwing this error:
undefined method `assessments_path' for #ActionView::Base:0x00000000229750
Did you mean? asset_path
couple of points:
in routes 'poweruser' is a namespace
in models assessment.rb is
not folder poweruser (as I plan to use it for other pages that anyone can access not just powerusers).
Any ideas what I am missing?
Here is the code:
<%= form_with(model: [#assessment], local: true) do |form| %>
<%= render "shared/error_messages", resource: form.object %>
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control" %>
</div>
<% end %>
Here is the controller
module Powerusers
class AssessmentsController < ApplicationController
before_action :authenticate_user!
# before_action :set_assessment
# before_action :set_assessment, only: [:set_assessment, :show, :edit, :update, :destroy]
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# super
# send_foo_updated_email(requested_resource)
# end
def index
#pagy, #assessments = pagy(Assessment.sort_by_params(params[:sort], sort_direction))
# We explicitly load the records to avoid triggering multiple DB calls in the views when checking if records exist and iterating over them.
# Calling #assessments.any? in the view will use the loaded records to check existence instead of making an extra DB call.
#assessments.load
end
# GET /assessments/new
def new
#assessment = Assessment.new
#assessment.assessment_sections.new
end
here is the assessment.rb (not it is not in a sub-folder poweruser)
class Assessment < ApplicationRecord
belongs_to :company
has_many :assessment_sections, inverse_of: :assessment
has_many :questions, through: :assessment_sections
accepts_nested_attributes_for :assessment_sections, reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :questions, reject_if: :all_blank,
allow_destroy: true
end
Here is the routes
# PowerUser
authenticated :user, lambda { |u| u.admin? } do
namespace :powerusers do
resources :assessments do
resources :assessment_sections do
resources :questions
end
end
end
end
As you have namespace in routes, you need to specify that with form_with
<%= form_with(model: [:powerusers, #assessment], local: true) do |form| %>

Files saved by carrierwave is being gotten visible to users not logged in

Current Detail
[Env]
nginx puma mysql
Rails == 5.1.5
I mounted carrierwave to upload pictures and files in a post.
[Case]
Users not logged in type the file URL in browser, then they can access and view the file.
[Ideal]
Only users logged in is accessible to the file.
The file path is "uploads/post/images/1234(post_id)/sample.png".
So far, I locate uploads directory under public, app/assets/, and root directory in vain.
Any answers or suggestions are appreciated.
Source
Rails.application.routes.draw do
get 'users/index'
get 'users/show'
get 'posts/index'
devise_for :users, module: :users
resources :users, :only => [:index, :show]
get "/" => "posts#index"
get "posts/like_ranking" => "posts#like_rank"
get "posts/post_count_ranking" => "posts#post_count"
get "posts/tags_search" => "posts#tags_search"
get "posts/new" => "posts#new"
get "posts/:id/reply" => "posts#new"
post "posts/create" => "posts#create"
get "posts/:id" => "posts#show"
get "posts/:id/edit" => "posts#edit"
post "posts/:id/update" => "posts#update"
post "posts/:id/destroy" => "posts#destroy"
get 'tags/:tag', to: 'posts#index', as: :tag
get "users/:id/likes" => "users#likes"
get "users/:id/reply" => "users#reply"
resources :posts, only: %w(index)
resources :posts, shallow: true do
resources :likes, only: [:create, :destroy]
end
end
"
class ImageUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
All assets under the public folder is served by nginx. In order to make it available for logged users you can first upload files to other directory than public and create action which serve files by Rails app from that directory and check if users logged before serving.

mysql Databases how can I fix this?

Hi Im trying to set up a database to store emails using ruby, sinatra, ActiveRecord, and mysql. Any suggestions on what im doing wrong? Im trying to output it to a seperate page that only i can see and then post it using a url on hostgator.
require 'sinatra'
require 'activerecord'
# require 'sinatra-activerecord'
get '/' do
erb :index
end
def save_email (email)
file.print(email)
end
get '/email' do
params[:email]
# # redirect '/'
end
post '/email' do
params[:email]
#email = params[:email]
erb :email, :locals => {:email => params[:email]}
end
# Change the following to reflect your database settings
ActiveRecord::Base.establish_connection(
adapter: 'mysql', # or 'postgresql'
host: 'localhost',
database: 'Stored_Emails',
)
class Stored_Emails < Sinatra::Application
end
class Stored_Emails < ActiveRecord::Base
end
ActiveRecord::Migration.create_table :email do |t|
t.string :emails
end
create_table :emails, force: true do |t|
t.string :email
t.belongs_to :email, index: true
end
get '/email' do
params[:email].all
end
Typically you break out your code into multiple files (we use folders named config, helpers, libraries, views, routes, models, migrations) and require them at the top of your app. However, if you want to put it in the same file and just use that and a Gemfile and Gemfile.lock that'll work too. Here's how it might look:
# Require your gems
require 'sinatra'
require 'activerecord'
# Libraries
# Models
class Stored_Emails < ActiveRecord::Base
end
# Configuration
# Change the following to reflect your database settings
ActiveRecord::Base.establish_connection(
adapter: 'mysql', # or 'postgresql'
host: 'localhost',
database: 'Stored_Emails'
)
ActiveRecord::Migration.create_table :email do |t|
t.string :emails
end
# Migrations
create_table :emails, force: true do |t|
t.string :email
end
# Helpers
def save_email (email)
file.print(email)
end
# Routes
get '/' do
# Load whatever you want to show in your index page into class variables
erb :index
end
get '/email' do
Stored_Emails.all.to_json
end
post '/email' do
#email = Stored_Emails.find_by(params[:email])
erb :email
end
Now you're going to have to do a good bit of work to get this running. Here's what I suggest you read:
1) Sinatra documentation - http://www.sinatrarb.com/intro.html
routes
running your sinatra app
views with ERB
2) Bundler documentation for gems - http://bundler.io/
3) ActiveRecord documentation - http://guides.rubyonrails.org/active_record_basics.html
Connecting to a database Creating your database with a migration -
this is a one time deal Querying the database
Good Luck!

Legacy Database CarrierWave Integration

I am having legacy database
Rails 3.2 & ruby 1.9.3p448
I wanted to develop active_admin back-end application
I wanted use CarrierWave File Uploads gem for uploading image
Following is my code in admin/item_master.rb i.e in view
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Item Details" do
f.input :IMTNAME
f.input :IMTBRIEFDESC
f.input :IMTDETAILDESC
f.input :IMTIMAGE, :as => :file
end
f.actions
end
Following is my code in model
class ItemMaster < ActiveRecord::Base
set_table_name "MDIMT"
attr_accessible :IMTNAME, :IMTBRIEFDESC, :IMTDETAILDESC, :IMTIMAGE
mount_uploader :IMTIMAGE, ImtimageUploader
end
It givesme Following Error
NameError in Admin::ItemMastersController#update
uninitialized constant IMTIMAGE

undefined local variable or method `acts_as_gmappable'

I created a Heroku app which utilizes the gmaps4fails gem for one of the application's features.
When I tried to seed the database, I get
rake aborted!
undefined local variable or method 'acts_as_gmappable' for #<Class:0x00000004b5b928>
Any ideas how to fix this?
Everything works fine when testing locally.
Edit: Here's my model code
class Hall < ActiveRecord::Base
has_many :hall_features
has_many :green_features, :through => :hall_features
has_many :settings, :through => :pinned_halls
belongs_to :operational_unit
acts_as_gmappable
...
Migration:
class CreateHalls < ActiveRecord::Migration
def up
create_table 'halls' do |t|
t.string 'name'
t.text 'background'
t.text 'energy_info'
t.float 'latitude'
t.float 'longitude'
t.boolean 'gmaps'
t.timestamps
end
end
def down
drop_table 'halls' # deletes the whole table and all its data!
end
end
Here's the error from my terminal: http://i48.tinypic.com/zvoggg.png
Ugh .. put the gem under :development in Gemfile only. Including it in :production solved everything.
I'll be crying myself to sleep tonight. Some tears of happiness too.