Friendly_Id record not found - activerecord

I get a record not found exception using friendly_id 5.0 stable with Rails 4.0
Error:
Migration:
class AddSlugToUsers < ActiveRecord::Migration
def change
add_column :users, :slug, :string
add_index :users, :slug
end
end
Controller:
class UsersController < ApplicationController
load_and_authorize_resource
before_filter :authenticate_user!
def index
authorize! :index, #user, :message => 'Not authorized as an administrator.'
#users = User.all
end
def show
##user = User.find(params[:id])
#user = User.friendly.find(params[:id])
end
def update
authorize! :update, #user, :message => 'Not authorized as an administrator.'
#user = User.find(params[:id])
if #user.update_attributes(params[:user], :as => :admin)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
authorize! :destroy, #user, :message => 'Not authorized as an administrator.'
user = User.find(params[:id])
unless user == current_user
user.destroy
redirect_to users_path, :notice => "User deleted."
else
redirect_to users_path, :notice => "Can't delete yourself."
end
end
end
The Data:
INSERT INTO
users(id,email,encrypted_password,reset_password_token,reset_password_sent_at,remember_created_at,
sign_in_count,current_sign_in_at,last_sign_in_at,current_sign_in_ip,last_sign_in_ip,
created_at,updated_at,first_name,last_name,alias,bio,slug)
VALUES
(10,'me1#example.com','$2a$10$MYHASG','',null,null,0,null,null,'','',
Invalid Date,Invalid Date,'greek','god','tool','','tool');
It works if I put the ID into the url
http://0.0.0.0:3000/users/10
but does not work when using the slug
http://0.0.0.0:3000/users/tool

The quickest fix is to use the old 4-style finders as described in the readme by using the :finders addon.
Then you'll have friendly-id access via the "normal" find() method.
Example:
friendly_id :foo, use: [:slugged, :finders] # you can now do MyClass.find('bar')

Related

Rails - Unknown Attribute - Unable to add a new field to a form on create/update

Hello I am new to Rails.
Here is my controller/projects_controller.rb code
def create
#project = current_user.projects.new(project_params)
##project.website = params[:website]
respond_to do |format|
if #project.save
Member.create!(
user_id: current_user.id,
project_id: #project.id,
project_manager: true,
status: "ready")
format.html { redirect_to #project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def update
##project.website = params[:website]
respond_to do |format|
# if (#project.active_was == true) &&
# disabled = true
# end
if #project.update(project_params)
# if disabled && (#project.active == false)
# flash[:modal] = true
# end
#project.website = params[:website]
format.html { redirect_to #project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def project_params
params.require(:project).permit(
:university_id,
:project_name,
:location,
:tagline,
:photos,
:industry,
:category,
:description,
:category_id,
:expertise_string,
:website,
:active,
:slug,
:project_groups_attributes => [:id, :active, :university_id, :user_group_id]
)
end
Here is the code for my model/project.rb
class Project < ActiveRecord::Base
# has_and_belongs_to_many :users
has_many :members, :dependent => :destroy
has_many :users, :through => :members
has_one :survey
has_many :project_groups, foreign_key: "project_id", dependent: :destroy
has_many :groups, :through => :project_groups, :source => :university
accepts_nested_attributes_for :project_groups, :allow_destroy => true
has_many :project_expertises, foreign_key: "project_id",
dependent: :destroy
has_many :expertises, :through => :project_expertises, :source => :expertise
belongs_to :category
belongs_to :website
Here is my db/migrate/[timestamp]_create_projects.rb code
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.integer :university_id
t.string :project_name
t.string :tagline
t.string :photos
t.string :industry
t.integer :category
t.text :description
t.text :website
t.timestamps
end
end
end
Why can I not add the 'website' field to this? Is there something more that I have to do?
As per your comment there is not any Website model available so there is not any meaning of putting association:
Simply remove this line from your project model
belongs_to :website
In Project model you've specified relation belongs_to :website. In migration schema instead of:
t.text :website
must be something like that:
t.references :website, index: true, foreign_key: true
When you generate model instead of
rails g ... website:text use rails g ... website:references.
ActiveRecord can't find foreign key to associate Project with Website.

New password could not set into DB using Ruby on Rails 3

I am trying to reset the new password using Rails 3.When i insert new password(i.e.123456789) and clicked on submit button it gave me the following error.
Error:
NoMethodError in HomesController#resubmitpass
undefined method `stringify_keys' for "123456789":String
Rails.root: C:/Site/library_management1
Application Trace | Framework Trace | Full Trace
app/controllers/homes_controller.rb:179:in `resubmitpass'
Please check my below codes and let me to know how it will be solved ?
views/homes/resetpass.html.erb
<center>
<%= form_for :users,:url => {:action =>'resubmitpass',:id =>params[:id] } do |f| %>
<div class="pass-div">
<p>
<%= f.password_field :password,:id => "pre-pass",placeholder:"Enter your new password" %>
</p>
<p>
<%= f.password_field :password_confirmation ,:id => "pre-pass", placeholder:"Enter your password again" %>
</p>
<p>
<%= f.submit 'Update',:class => "btn btn-success" %>
</p>
</div>
<% end %>
</center>
controller/homes_controller.rb
class HomesController < ApplicationController
before_filter :authenticate_admin!,only: [:admin]
def index
end
def admin
end
def managebooks
#books=Book.new
if params[:id]
#books=Book.find(params[:id])
#book=Book.all
end
end
def savebooks
#books=Book.new(params[:books])
if #books.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'managebooks',:id => #books.id
else
flash[:notice]="Data couldnot submitted successfully"
flash[:color]="invalid"
render 'managebooks'
end
end
def remove
#books=Book.find(params[:id])
#books.destroy
end
def books
end
def showbooks
#books=Book.all
end
def searchbooks
#books=Book.all
end
def member
#users=User.new
end
def registration
#users=User.new
end
def savedata
#users=User.new(params[:users])
if #users.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data could not submitted successfully"
flash[:color]="invalid"
render 'registration'
end
end
def issuebooks
#issues=Issue.new
end
def savedissuebooks
#issues=Issue.new(params[:issues])
if #issues.save
flash[:notice]="information has saved successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data couldnot saved"
flash[:color]="invalid"
render 'issuebooks'
end
end
def availablebooks
#books=Book.all
end
def userissues
#issues=Issue.all
end
def magazine
#magazines=Magazine.new
end
def savemagazines
#users=User.find(params[:id])
#magazines=Magazine.new(params[:magazines])
#magazines.user_id=#users.id
if #magazines.save
flash[:notice]="Data submitted successfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:notice]="Data could not saved"
flash[:color]="invalid"
render 'magazines'
end
end
def magazineissue
#magazines=Magazine.all
#users=User.find #magazines.first.user_id
end
def blog
#blogs=Blog.new
end
def savecomments
#users=User.find(params[:id])
#blogs=Blog.new(params[:blogs])
#blogs.user_id=#users.id
if #blogs.save
flash[:notice]="Comment has been posted successfully"
flash[:color]="valid"
redirect_to :action => "showcomment"
else
flash[:notice]="Comment could not saved"
flash[:color]="invalid"
render 'blog'
end
end
def showcomment
#blogs=Blog.all
end
def newspaper
#newspapers=Newspaper.new
end
def savenewspaper
#users=User.find(params[:id])
#newspapers=Newspaper.new(params[:newspapers])
#newspapers.user_id=#users.id
if #newspapers.save
flash[:notice]="newspaper data saved successfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:alert]="Data could not saved successfully"
flash[:color]="invalid"
render 'newspaper'
end
end
def adminnewspaperissue
#newspapers=Newspaper.all
#users=User.find #newspapers.first.user_id
end
def userprofile
#users=User.find(params[:id])
end
def updatedata
#users=User.find(params[:id])
if #users.update_attributes(params[:users])
flash[:notice]="User Data has updated"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:alert]="Data could not updated"
flash[:color]="invalid"
render 'userprofile'
end
end
def forgetpass
#users=User.new
end
def sendemail
#users=User.find_by_email(params[:users][:email])
if #users.email==params[:users][:email]
UserMailer.registration_confirmation(#users).deliver
flash[:notice]="check your mail to reset your password"
flash[:color]="valid"
redirect_to :action => "checkmail"
else
flash[:alert]="You might entered wrong email address"
flash[:color]="invalid"
render 'forgetpass'
end
end
def resetpass
#users=User.new
end
def resubmitpass
#users=User.find(params[:id])
if #users.update_attributes(params[:users][:password])
flash[:notice]="Your password updated succesfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:alert]="Your password could not updated successfully"
flash[:color]="invalid"
render 'resetpass'
end
end
end
model/user.rb
class User < ActiveRecord::Base
attr_accessible :address, :email, :first_name, :last_name, :password, :password_hash, :password_salt, :tel_no ,:password_confirmation
attr_accessor :password
before_save :encrypt_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :first_name, :presence => true, :length => {:in => 3..10}
validates :last_name , :presence => true , :length => {:in => 3..10}
validates :tel_no , :presence => true , :length => {:in => 1..10}
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
has_many :issue
has_many :book
has_many :magazine
has_many :blog
has_many :newspaper
end
db/migrate/20150311112733_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password
t.string :tel_no
t.string :address
t.string :password_hash
t.string :password_salt
t.timestamps
end
end
end
Actually the error is coming in this line "if #users.update_attributes(params[:users][:password])".Please help me to resolve this error.
It is happening because you are using update_attributes incorrectly.
You need to specifically pass the params to the associated column like update_attributes(:password => params[:password]) or better use Object.update_attribute(:only_one_field, "Some Value")
If you want to pass the whole params hash you can simply use this:
update_attributes(params[:user]).
You can read more about it here.

undefined method `plantposts' for nil:NilClass

This is my first app since finishing the tutorial at railstutorial.org and everything was going well until this. In my app each user can create plants and each plant can have many plantposts. I have put a form to create new plantposts on the plants/id (show) page which I cannot get working. I suspect it may have to do with the routes file.
Here are my controllers:
class PlantpostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
##plantpost = Plantpost.new(plantpost_params)
##plant = Plant.find(params[:plant_id])
#plantpost = #plant.plantposts.build(plantpost_params) #this seems to be the main problem line.
if #plantpost.save
flash[:success] = "Plantpost created!"
redirect_to plant_path(#plantpost.plant_id)
else
flash[:danger] = "No Plantpost created!"
#plantpost_feed_items = []
redirect_to plant_path(id: params[:plant_id] )
end
end
def destroy
#plantpost.destroy
flash[:success] = "Plantpost deleted"
redirect_to request.referrer || root_url
end
private
def plantpost_params
params.require(:plantpost).permit(:content, :picture, :user_id, :plant_id)
end
def correct_user
#plantpost = current_user.plantposts.find_by(id: params[:id])
redirect_to root_url if #plantpost.nil?
end
end
My Plants Controller
class PlantsController < ApplicationController
before_action :logged_in_user, only: [:index, :create, :edit, :update,
:destroy, :show]
before_action :correct_user, only: [:destroy, :update ]
def new
#plant = Plant.new
end
def index
#plant_feed_items = current_user.plants.paginate(page: params[:page], :per_page => 10)
#plants = current_user.plants.all
#plant = Plant.new
end
def show
#plant = Plant.find(params[:id])
#plantpost_feed_items = #plant.plantpost_feed.paginate(page: params[:page], :per_page => 10)
#plantposts = #plant.plantposts.paginate(page: params[:page])
#plantpost = Plantpost.new
end
private
def plant_params
params.require(:plant).permit( :notes, :plantname,:source, :planted, :harvested, :yield, :yieldunits)
end
def correct_user
#plant = current_user.plants.find_by(id: params[:id])
redirect_to root_url if #plant.nil?
end
def plantpost_params
params.require(:plantpost).permit(:content, :picture, :user_id, :plant_id)
end
The relevant parts of the model relationships
class User < ActiveRecord::Base
has_many :plants, dependent: :destroy
has_many :plantposts, through: :plants, dependent: :destroy
class Plant < ActiveRecord::Base
has_many :plantposts, dependent: :destroy
belongs_to :user
class Plantpost < ActiveRecord::Base
belongs_to :plant
belongs_to :user
And the routes file
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :plantposts, only: [:create, :destroy]
resources :plants
#post 'plants' => 'plantposts#create'
end
As you can see from all the hashtags I have been trying a lot of combinations hoping to stumble across the right one.
I have also written an integration test in which this line " post plant_path(#plant.id), plantpost: { content: "" }" gets this response
ERROR["test_plantpost_interface", PlantpostInterfaceTest, 1.515159549]
test_plantpost_interface#PlantpostInterfaceTest (1.52s)
ActionController::RoutingError: ActionController::RoutingError: No route matches [POST] >"/plants/980190962"
test/integration/plantposts_interface_test.rb:16:in block in
<class:PlantpostInterfaceTest>'
test/integration/plantposts_interface_test.rb:16:inblock in '
It seems like what you want to do is nest the resources.
In your routes file, change
resources :plantposts, only: [:create, :destroy]
resources :plants
to
resources :plants do
resources :plantposts, only: [:create, :destroy]
end
This turned out to be a syntax problem. The working create code is:
def create
#plant = Plant.find_by(id: params[:plant_id] )
#plantpost = #plant.plantposts.build({:plant_id => params[:plant_id], :user_id => current_user.id, :content => params[:plantpost][:content], :picture => params[:plantpost][:picture]})
if #plantpost.save
flash[:success] = "Plantpost created!"
redirect_to plant_path :id => #plant.id
else
#plantpost_feed_items = []
render 'plants/show', :id => #plant.id
end
end
I'm sure there is a cleaner way to do it but I'm moving on.

Unpermitted parameters - Rails 4.1.1, Ruby 2.1.2

I cannot seem to get to the bottom of where I am going wrong. My "order.rb" fields populate ok, but I can't get the "order_row" table values to populate. I just keep getting the following error in terminal(not worried about date for now, should be ok with that)...
Unpermitted parameters: date(i), order_row
Customer model(customer.rb)...
class Customer < ActiveRecord::Base
has_many :orders, dependent: :destroy
end
Order model(order.rb)...
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_rows, dependent: :destroy
accepts_nested_attributes_for :order_rows
end
Order_Row model(order_row.rb)
class OrderRow < ActiveRecord::Base
belongs_to :order
end
(orders_controller.rb)....
def new
#order = Order.new
end
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to(#order, :notice => 'Order was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
private
def order_params
params.require(:order).permit(:customer_id, :date, :total,
:order_row_attributes => [:description, :quantity, :price, :order_id])
end
Form code on new.html.haml
= semantic_form_for #order do |f|
= f.input :customer_id, :as => :select, :collection => Hash[Customer.all.map{|c| [c.company,c.id]}]
= f.input :date
= f.fields_for :order_row do |ff|
= ff.input :description
= ff.input :quantity
= ff.input :price
= ff.hidden_field :order_id
= f.input :total
= f.action :submit, :as => :button
The problem is this line order_row_attributes.It should be order_rows_attributes. And with the date not being permitted,try changing the date attribute to some name like order_date.
This should work
private
def order_params
params.require(:order).permit(:customer_id, :order_date, :total,
:order_rows_attributes => [:description, :quantity, :price, :order_id])
end
I got it working by changing the new method to....
def new
#order = Order.new
#order.order_rows.build
end
So combination of this and Pavans answer did the trick.

How to access param values in controller in ruby on rails?

I got a task to access param values for creating new user.My controller code for create is
def newstudent
#student = Student.new(params)
puts "+++++++++++"
puts params.inspect
if #student.save
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #jtable }
end
end
end
But by doing this i had got some error in terminal.It shows like this
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: action, controller):
app/controllers/students_controller.rb:42:in `new'
app/controllers/students_controller.rb:42:in `newstudent'
Please help me to solve the problem?
This is my controller for add new student. By getting that error message you must reject controller and action using #student = Student.new(params.reject {|key,value| key =="controller" || key =="action"}) code.
def newstudent
#student = Student.new(params.reject {|key,value| key =="controller" || key =="action"})
if #student.save
#jtable = {'Result' => 'OK','Record' => #student.attributes}
else
#jtable = {'Result' => 'ERROR','Message'=>'Empty'}
end
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #jtable }
end
end
In rails 3.2.3 there is no mass assignment by default. You have to go your model and add attr_accessible :name, :position, :visible. Basically you have to add every attribute you want to mass assign.
class Student < ActiveRecord::Base
attr_accessible :name, :position, :visible
end
Your Student model should whitelist the attributes that can be mass-assigned using attr_accessible as in:
class Student < ActiveRecord::Base
attr_accessible :name
end

Resources