v3.2.1
Not sure why 'count' is coming up nil and index won't render, as 'count' it's been fine in every model until I do the uniqueness with scope validation.
Any suggestions?
MODEL
Class FeatureIcon < ActiveRecord::Base
belongs_to :user
validates_presence_of :img_size, :feature_name, :image, :user_id
validates_uniqueness_of :img_size, :scope => :feature_name
//paperclip interpolates stuff....
end
CONTROLLER
before_filter :load_user
def index
#feature_icons = #user.feature_icons.all
#feature_icon = #user.feature_icons.new
respond_to do |format|
format.html # index.html.erb
format.json { render json: #feature_icons }
format.js
end
end
def create
#feature_icon = #user.feature_icons.new(params[:feature_icon])
respond_to do |format|
if #feature_icon.save
format.html { redirect_to user_feature_icons_url, notice: 'successfully created.' }
format.json { render json: #feature_icon, status: :created, location: #feature_icon }
format.js
else
format.html { render action: "index" }
format.json { render json: #feature_icon.errors, status: :unprocessable_entity }
end
end
end
ERROR
NoMethodError in Feature_icons#create
undefined method `count' for nil:NilClass
Extracted source (around line #7):
6: <div class="count">
7: <div id="count" class="feed-count"><%= #feature_icons.count %></div>
8: </div>
In the create method, you instanciate #feature_icons (with an 's'), but in the view you're using #feature_icon (without the 's'), so #feature_icons is nil.
If the save fails, the line format.html { render action: "index" } renders the view index.htm.erb, but the method index in the controler is not not called. Try with
if #feature_icon.save
#... nothing to change
else
format.html do
#feature_icons = #user.feature_icons.all
render action: "index"
end
format.json { render json: #feature_icon.errors, status: :unprocessable_entity }
end
or
if #feature_icon.save
#... nothing to change
else
format.html { redirect_to :index }
format.json { render json: #feature_icon.errors, status: :unprocessable_entity }
end
Related
I'm having some issues with Overriding Named Route Parameters when I edit or create a post I get an error undefined method playerId for nil:NilClass. It still re-directs to the :id instead of the :playerId params only with create and edit methods.
Below, :playerId should be 101, but the 6 is the :id, not sure why it's picking it up.
SELECT `players`.* FROM `players` WHERE `players`.`playerId` = 6 LIMIT 1 [["playerId", "6"]]
Routes
resources :players, param: :playerId
Controller
def show
#player = Player.find_by(playerId: params[:playerId])
#season = PlayerStat.where("playerId = ?", #player.playerId).joins(:matches).where('matches.gameType = ?', 0).where('matches.teamId = ?', #player.teamId).group('year(matches.matchDate) DESC')
end
def edit
end
def create
#player = Player.new(player_params)
respond_to do |format|
if #player.save
format.html { redirect_to #player, notice: 'PLayer was successfully created.' }
format.json { render :show, status: :created, location: #player }
else
format.html { render :new }
format.json { render json: #player.errors, status: :unprocessable_entity }
end
end
end
def update
#player = Player.find params[:playerId]
respond_to do |format|
if #player.update(player_params)
format.html { redirect_to #player, notice: 'Player was successfully updated.' }
format.json { render :show, status: :ok, location: #player }
else
format.html { render :edit }
format.json { render json: #player.errors, status: :unprocessable_entity }
end
end
end
private
def set_player
#player = Player.find_by(playerId: params[:playerId])
end
def player_params
params.require(:player).permit(:playerId, :first_name, :last_name, :dob, :teamId, :jumper_no, :height, :weight, :image, team_attributes: [:teamId, :name], player_stats_attributes: [:playerId, :gameDate, :kicks, :marks])
end
undefined method playerId for nil:NilClass
The problem is params[:layerId] is nil upon a successful create or update because you aren't passing any playerId for the redirect_to. So #player is nil which resulted in that error. Changing your code to below should fix the error.
format.html { redirect_to player_path(#player.playerId), notice: 'PLayer was successfully created.' }
Same for update too.
you can define full routes like this:
get '/player/:playerId' => 'players#show'
get '/player/:playerId/edit' => 'players#edit'
I need to save items separately coming from a form of a text field, but my code is saving these items duplicate form.
My controller
def create
#answer_option = AnswerOption.break_options(answer_option_params)
#answer_option = AnswerOption.new(answer_option_params)
respond_to do |format|
if #answer_option.save
format.html { redirect_to #answer_option, notice: 'Answer option was successfully created.' }
format.json { render :show, status: :created, location: #answer_option }
else
format.html { render :new }
format.json { render json: #answer_option.errors, status: :unprocessable_entity }
end
end
end
My model
class AnswerOption < ActiveRecord::Base
belongs_to :question
def self.break_options(var)
ugly_answers = var[:content].split /[\r\n]+/
ugly_answers.each do |answer|
AnswerOption.create!(content: answer)
end
end
end
Thanks!
def create
#answer_option = AnswerOption.break_options(answer_option_params)
end
I have this code that validates by user_id. If the category_name exists, this prevents the creation of a new Object. The code works but I don't believe this is best practice.
def create
#item_category = ItemCategory.new(item_category_params)
#item_category.user_id = current_user.id
search = ItemCategory.where(:name => #item_category.name,:user_id => current_user.user_id)
if search.blank?
respond_to do |format|
if #item_category.save
format.html { redirect_to :back, notice: 'Se ha creado la categoria' }
format.json { render action: 'show', status: :created, location: #item_category }
else
format.html { render action: 'new' }
format.json { render json: #item_category.errors, status: :unprocessable_entity }
end
end
else
respond_to do |duplicate|
duplicate.html { redirect_to #item_category, alert: 'Categoria Repetida' }
duplicate.json { render json: #item_category.errors, status: :unprocessable_entity}
end
end
end
Thanks.
It would be better to put the validation in your ItemCategory model...
class ItemCategory < ActiveRecord::Base
validates_uniqueness_of :name, :scope => :user_id
...
end
I have a problem with simple model validation.
Entering materials without SKU is getting me error message:
NoMethodError in Materials#create undefined method `empty?' for
nil:NilClass
material.rb:
class Material < ActiveRecord::Base
validates :sku, :presence => true
end
materials_controler.rb (create part only):
# POST /materials
# POST /materials.json
def create
#material = Material.new(material_params)
respond_to do |format|
if #material.save
format.html { redirect_to #material, notice: 'Material was successfully created.' }
format.json { render :show, status: :created, location: #material }
else
format.html { render :new }
format.json { render json: #material.errors, status: :unprocessable_entity }
end
end
end
Your #units instance variable is nil in create action. You should set it the same way as you do in new or edit actions after your records fails validation.
Define #units also in your create method
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