I have a module that returns an array:
module Module1
class Class1
def self.get
num << 1
return num
end
end
end
But when I call it from the controller like this:
def index
#trans = Module1::Class1.get()
respond_to do |format|
format.html # index.html.erb
format.json { render #trans }
end
end
Show me the following error:
'1' is not an ActiveModel-compatible object that returns a valid partial path.
But if I do in json:
def index
respond_to do |format|
format.html # index.html.erb
format.json { render Module1::Class1.get() }
end
end
It returns the right result, what am I doing wrong in the first example?
Try this
format.json { render :json => #trans }
Related
In my application, if the User must deselect all check box him informed, be saved or a null Array [].
I'm using params[: parameter1] [: parametro2_ids] | | = [] after booting my Action UPDATE, however if I try to clear all the rails in the log returns:
NoMethodError (undefined method '[]' for nil: NilClass):. In normal logic, it should be functional. How can I implement a function if it is not checked or unchecked all worthless?
responsabilities_controller
class ResponsabilitiesController < ApplicationController
def index
#responsabilities = Responsability.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #responsabilities }
end
end
def show
#responsability = Responsability.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #responsability }
end
end
def new
#responsability = Responsability.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #responsability }
end
end
def edit
#responsability = Responsability.find(params[:id])
end
def create
#responsability = Responsability.new(params[:responsability])
respond_to do |format|
if #responsability.save
format.html {
flash[:notice] = l(:notice_success)
redirect_to :contoller => "uc_rh",:action => "index"
}
else
format.html { render action: "new" }
format.json { render json: #responsability.errors, status: :unprocessable_entity }
end
end
end
def update
params[:responsability][:knowledge_ids] || [] if params[:responsability].present?
params[:responsability][:competence_ids] || [] if params[:responsability].present?
params[:responsability][:tool_ids] || [] if params[:responsability].present?
#responsability = Responsability.find(params[:id])
respond_to do |format|
if #responsability.update_attributes(params[:responsability])
format.html {
flash[:notice] = l(:notice_success_edit)
redirect_to :contoller => "responsabilities",:action => "edit"
}
else
format.html { render action: "edit" }
format.json { render json: #responsability.errors, status: :unprocessable_entity }
end
end
end
def destroy
#responsability = Responsability.find(params[:id])
#responsability.destroy
respond_to do |format|
format.html { redirect_to responsabilities_url }
format.json { head :no_content }
end
end
def nested_knowledges
#responsability = Responsability.find(params[:id])
end
def nested_competences
#responsability = Responsability.find(params[:id])
end
def nested_tools
#responsability = Responsability.find(params[:id])
end
end
try using this code
params[:parameter1][:parametro2_ids] ||= [] if params[:parameter1].present?
If I enter the following URL in a browser, I get a list of client records:
http://localhost:5000/clients.json
Now, I would like to be able the select certain clients via the URL. Is something like this possible:
http://localhost:5000/clients.json?locname=ys
This is my clients_controller.rb for index:
def index
#clients = Client.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #clients }
end
end
THANKS!
This should do the job:
def index
#clients = Client.scoped
#clients = #clients.where(:locname => params[:locname]) if params[:locname].present?
respond_to do |format|
format.html # index.html.erb
format.json { render json: #clients }
end
end
In my application, I want to only allow user with admin privilege to access this model. So I set up and before_filter to check if the user is an Admin. The problem with this approach is that, after the admin user passes the filter, s/he won't be able to get redirect to the action. Instead, only the views are rendered, which leads to the undefined method each' for nil:NilClass error. What am I doing wrong here?
class TidbitsController < ApplicationController
before_filter :is_admin?
layout "tidbits"
# GET /tidbits
# GET /tidbits.json
protected
def is_admin?
unless current_user.admin?
flash[:error] = "You are not authorized!"
redirect_to "/" and return
end
end
def index
#tidbits = Tidbit.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #tidbits }
end
end
# GET /tidbits/1
# GET /tidbits/1.json
def show
#tidbit = Tidbit.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #tidbit }
end
end
# GET /tidbits/new
# GET /tidbits/new.json
def new
#tidbit = Tidbit.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #tidbit }
end
end
# GET /tidbits/1/edit
def edit
#tidbit = Tidbit.find(params[:id])
end
# POST /tidbits
# POST /tidbits.json
def create
#tidbit = Tidbit.new(params[:tidbit])
respond_to do |format|
if #tidbit.save
format.html { redirect_to #tidbit, notice: 'Tidbit was successfully created.' }
format.json { render json: #tidbit, status: :created, location: #tidbit }
else
format.html { render action: "new" }
format.json { render json: #tidbit.errors, status: :unprocessable_entity }
end
end
end
# PUT /tidbits/1
# PUT /tidbits/1.json
def update
#tidbit = Tidbit.find(params[:id])
respond_to do |format|
if #tidbit.update_attributes(params[:tidbit])
format.html { redirect_to #tidbit, notice: 'Tidbit was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #tidbit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tidbits/1
# DELETE /tidbits/1.json
def destroy
#tidbit = Tidbit.find(params[:id])
#tidbit.destroy
respond_to do |format|
format.html { redirect_to tidbits_url }
format.json { head :no_content }
end
end
end
in your example all your action methods are protected so maybe that's the problem?
I think you forgot to add the devise required callback filter
before_filter :authenticate_user!
before_filter :is_admin?
When I press new on my Jobs form in seeing an error that it could not find 'create' in my JobsController.
Unknown action
The action 'create' could not be found for JobsController
Here' my controller:
class JobsController < ApplicationController
private
def load_clients
#clients = collection_select :client, :client_id
end
def index
#job = Job.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #job }
end
end
def create
#job = Job.new(params[:job])
respond_to do |format|
if #job.save
format.html { redirect_to #job, notice: 'Job was successfully created.' }
format.json { render json: #job, status: :created, location: #job }
else
format.html { render action: "new" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
def show
#job = Job.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #job }
end
end
end
As you can see. It' clearly there. Why is Rails not seeing it?
It is because you assign the create method as private.
Try redefine your controller this way.
Here' my controller:
class JobsController < ApplicationController
def index
...
end
def create
...
end
def show
...
end
private
def load_clients
#clients = collection_select :client, :client_id
end
end
Hello guys I've 2 models: User(aka parent) and Profil(aka child).And I want to limit the number of profil for a user to one.
models/user.rb
class User < ActiveRecord::Base
has_one :profil
end
models/profil.rb
class Profil < ActiveRecord::Base
attr_accessible :logo
belongs_to :user
mount_uploader :logo, ImageUploader
validate :limit_profil_to_one, :on => :create
def limit_profil_to_one
if self.user.profils(:reload).count > 1
errors.add(:base, "Exceeded thing limit")
end
end
end
But when I try to create a profil I get the following error message:
NoMethodError (undefined method `profils' for nil:NilClass):
app/models/profil.rb:11:in `limit_profil_to_one'
app/controllers/profils_controller.rb:52:in `block in create'
app/controllers/profils_controller.rb:51:in `create
controllers/profils_controller.rb
# -*- encoding : utf-8 -*-
class ProfilsController < ApplicationController
# GET /factures
# GET /factures.json
def index
#profils = Profil.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #profil }
end
end
# GET /profils/1
# GET /profils/1.json
def show
#profil = Profil.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #profil }
end
end
# GET /profils/new
# GET /profils/new.json
def new
#profil = Profil.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #profil }
end
end
# GET /profils/1/edit
def edit
#profil = Profil.find(params[:id])
end
# POST /profils
# POST /profils.json
def create
#profil = Profil.new(params[:profil])
respond_to do |format|
if #profil.save
format.html { redirect_to #profil, notice: 'Profil was successfully created.' }
format.json { render json: #profil, status: :created, location: #profil }
else
format.html { render action: "new" }
format.json { render json: #profil.errors, status: :unprocessable_entity }
end
end
end
# PUT /profils/1
# PUT /profils/1.json
def update
#profil = Profil.find(params[:id])
respond_to do |format|
if #profil.update_attributes(params[:profil])
format.html { redirect_to #profil, notice: 'Profil was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #profil.errors, status: :unprocessable_entity }
end
end
end
# DELETE /factures/1
# DELETE /factures/1.json
def destroy
#profil = Profil.find(params[:id])
#profil.destroy
respond_to do |format|
format.html { redirect_to profils_url }
format.json { head :ok }
end
end
end
What I am doing wrong?
Look at line 2 in the limit_profil_to_one - self.user is nil so it is failing.
def limit_profil_to_one
if self.user.profils(:reload).count > 1 # self.user is nil
errors.add(:base, "Exceeded thing limit")
end
end
I am making some assumptions about what your app is doing, but for this post I am going to assume that your controller has a current user defined in the controller and that you are creating a Profil for that User (side: note, what is a profil? I am assuming you actually mean profile) You should set the user in the controller to the user it is supposed to be, like so.
def create
#profil = Profil.new(params[:profil])
#profil.user = current_user # however you access the currently logged in user
respond_to do |format|
if #profil.save
format.html { redirect_to #profil, notice: 'Profil was successfully created.' }
format.json { render json: #profil, status: :created, location: #profil }
else
format.html { render action: "new" }
format.json { render json: #profil.errors, status: :unprocessable_entity }
end
end
end