I'm trying to install this app on Windows 7 https://github.com/cheezy/puppies.
I am not a developer, I am just following below steps to install it:
Ruby Version ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32] installed on Windows 7
Steps:
Download zip from above link
Inside puppies-master do bundle update and then bundle install.
Then rails s
Launch localhost:3000
It gave error ruby : unsupported parameters: :order. I fixed it by
changing this line
#puppies = Puppy.paginate :page =>params[:page], :order => 'name', :per_page => 4 to this line
#puppies = Puppy.order(:name).paginate(page: params[:page], per_page: 4)
Error was gone and on localhost:3000 it showed me the app launched
But puppies (data) weren't there so I did
bundle && rake db:create && rake db:migrate and then rake db:seed
Refresh the localhost page and all puppies appeared but after I am not able to complete the transaction and on hitting Complete the adoption button it is giving me an error.
undefined method 'name' for nil:NilClass
If you just type undefined method 'name' for nil:NilClass in SOF you will get so many solutions but it seems different then those, because none worked for me.
This is what log says:
Started GET "/orders/new" for 127.0.0.1 at 2017-07-20 10:10:51
Processing by OrdersController#new as HTML
Cart Load (0.0ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/orders_controller.rb:23:in `new'
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (242.5ms)
Started GET "/orders/new" for 127.0.0.1 at 2017-07-20 11:20:21
Processing by OrdersController#new as HTML
Cart Load (0.0ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/orders_controller.rb:23:in `new'
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (252.0ms)
Edit:
orders_controller.rb
class OrdersController < ApplicationController
skip_before_filter :authorize, :only => [:new, :create]
def index
#orders = Order.paginate :page=>params[:page], :order=>'created_at desc',
:per_page => 10
respond_to do |format|
format.html # index.html.erb
end
end
def show
#order = Order.find(params[:id])
respond_to do |format|
format.html # show.html.erb
end
end
def new
#cart = current_cart
if current_cart.adoptions.empty?
redirect_to agency_url, :notice => "Your cart is empty"
return
end
#order = Order.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #order }
end
end
def edit
#order = Order.find(params[:id])
end
def create
#order = Order.new(params[:order])
#order.add_adoptions_from_cart(current_cart)
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to(agency_url, :notice => 'Thank you for adopting a puppy!') }
format.json { render :json => #order }
else
format.html { render :action => "new" }
end
end
end
def update
#order = Order.find(params[:id])
respond_to do |format|
if #order.update_attributes(params[:order])
format.html { redirect_to(#order, :notice => 'Order was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
def destroy
#order = Order.find(params[:id])
adoption = #order.adoptions.first
adoption.delivered_on = Time.now
adoption.save
respond_to do |format|
format.html { redirect_to(orders_url, :notice => "Please thank #{#order.name} for the order!") }
end
end
end
Edit2:
current_card method --
class ApplicationController < ActionController::Base
before_filter :authorize
protect_from_forgery
private
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
protected
def authorize
unless User.find_by_id(session[:user_id])
redirect_to login_url, :notice => "Please log in"
end
end
end
Related
I am trying to implement the functionality of creating a new article. However when I click the Submit button it gives me the following error.
Started POST "/articles" for 127.0.0.1 at 2017-12-23 14:04:29 +0500
Processing by ArticlesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lbrD/8QZAlLuxVbw3vmRuKI20zkrckYut4nebyW5hLk6EFkODh4d0IAZ6yms2oRkWGqE3eWlUQwBUrfu1SiAnw==", "article"=>{"title"=>"AYYE", "text"=>"Ma ka "}, "commit"=>"Create Article"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 13], ["LIMIT", 1]]
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.6ms)
NoMethodError (undefined method `user_id=' for nil:NilClass):
app/controllers/articles_controller.rb:26:in `create'
Here's the create method:
def create
#article.user_id = current_user.id
if #article.create
redirect_to #article
else
render 'new'
end
end
I am just starting out with RoR, hence unable to understand what's the problem here.
Apparently you #article is nil. It is throwing you error about calling/accessing user_id of nil object.
You need to make sure you define #article first, something like this
def create
#article = Article.new(article_params)
#article.user_id = current_user.id
if #article.save
redirect_to #article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
You have to create an instance of Article first. Then assign user_id and save the record.
def create
#article = Article.new
#article.user_id = current_user.id
#article.save
if #article.errors.any?
# error handling
else
render 'new'
end
end
I'm having trouble getting Best In Place to update. This is the first time I've used this gem or made a Rails app so I'm sure I'm just missing something simple even though I've scoured the documentation and searched through other questions here.
This is the error I get in the console:
Started PUT "/tasks/1" for 127.0.0.1 at 2016-04-22 11:52:10 -0600
Processing by TasksController#update as JSON
Parameters: {"authenticity_token"=>"cAwRbx8JeYDMG1LrR7nl0VQfwW/x00RUd8rsTP8Iwc
0=", "tasks"=>{"title"=>"Task 1sadsads"}, "id"=>"1"}
Task Load (0.0ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? ORD
ER BY priority ASC LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? ORDER B
Y priority ASC LIMIT 1 [["id", "1"]]
Completed 400 Bad Request in 3ms
ActionController::ParameterMissing (param is missing or the value is empty: task
):
app/controllers/tasks_controller.rb:99:in `task_params'
app/controllers/tasks_controller.rb:62:in `update'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (36.0ms)
My controller:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
respond_to do |type|
type.html
type.json {render :json => #task}
end
end
# GET /tasks/1
# GET /tasks/1.json
def show
#task = Task.find params[:id]
respond_to do |format|
format.html
format.json {render :json => #task}
end
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def sort
params[:order].each do |key,value|
Task.find(value[:id]).update_attribute(:priority,value[:position])
end
render :nothing => true
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
#task = Task.find params[:id]
if #task.update(task_params)
respond_to do |format|
format.html { redirect_to( #task )}
format.json { render :json => #task }
end
else
respond_to do |format|
format.html { render :action => :edit } # edit.html.erb
format.json { render :nothing => true }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:title, :description, :priority)
end
end
My view partial:
<div class="panel panel-default" data-id="<%= task.id %>">
<div class="panel-heading">
<h3 class="panel-title"><span class="rest-in-place" data-url="/tasks/<%= task.id %>" data-object="tasks" data-attribute="title" data-placeholder="Enter a title">
<%= task.title %>
</span></h3>
</div>
<div class="panel-body">
<%= truncate task.description, length: 50 %>
</div>
</div>
I really appreciate any help I can get. I'm 99% sure it's something I configured wrong in the controller, I just can't for the life of me figure out what it is.
EDIT: The background for this is that I have a bunch of 'Tasks' and I want to list them all on the index and be able to update any task on the index just by clicking on the title.
ActionController::ParameterMissing (param is missing or the value is empty: task
):
app/controllers/tasks_controller.rb:99:in `task_params'
app/controllers/tasks_controller.rb:62:in `update'
This is telling you that there is no parameter with the name of :task. You can see your parameters above this message.
Parameters: {"authenticity_token"=>"cAwRbx8JeYDMG1LrR7nl0VQfwW/x00RUd8rsTP8Iwc
0=", "tasks"=>{"title"=>"Task 1sadsads"}, "id"=>"1"}
You have tasks in your params here. This information is coming from your view.
data-object="tasks"
Try changing this piece in your view to task and it should send the request correctly.
Is my project class is nill or what, can any one explain me this error.
NoMethodError (undefined method projects' for nil:NilClass):
app/controllers/project_controller.rb:8:inindex'
Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb
(6.2ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.4ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(1.0ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout (50.0ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb
(0.4ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb
within layouts/inlined_string (0.3ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb
within layouts/inlined_string (0.3ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb
within layouts/inlined_string (0.4ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (41.3ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb
within layouts/javascript (0.3ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms) Rendered
/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (93.4ms)
Here is my project controller
before_action :confirm_logged_in
before_action :find_company
def index
#projects = #company.projects.sorted
end
def show
#project = Project.find(params[:id])
end
def new
#project = Project.new()
end
def create
#project = Project.new(project_params)
if #project.save
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#project = Project.find(params[:id])
end
def update
#project = Project.find(params[:id])
if #project.update_attributes(project_params)
redirect_to(:action => 'index')
else
render('new')
end
end
def delete
#project = Project.find(params[:id])
end
def destory
#project = Project.find(params[:id])
#project.destroy
redirect_to(:action => 'index')
end
private
def project_params
params.require(:project).permit(:name, :position, :type_of_project, :description, :no_of_tasks)
end
def find_company
if params[:company_id]
#company = Company.find(params[:company_id])
end
end
end
So actually what iam doing here is before entering in to projects we need to find the company_id and in index #projects = #company.projects.sorted means that company has many projects.
The problem is with the if params in the def find_company. For the index function this is not set.
change def index to:
def index
#projects = Project.sorted
end
UPDATE: made an error I think:
def index
#projects = Project.all.sorted #or leave the call to sorted out completely
end
I have a namespace called "backend" which is protected by Devise.
I would like now to allow users to edit their profil.
So I created a users_controller in Backend.
Here's the users_controllercode :
class Backend::UsersController < ApplicationController
layout 'admin'
before_filter :authenticate_user!
def index
#users = Backend::User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
end
def show
#user = Backend::User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #user }
end
end
def edit
#user = Backend::User.find(params[:id])
end
def update
#user = Backend::User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'Article was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
end
When I go on backend_users_path there is a list of all the users. I would like to permit to edit only his own profil.
So I go on the Edit page : <%= link_to "Edit", edit_backend_user_path(backend_user.id) %> .
Here's the Edit page code :
<%= simple_form_for #user do |f| %>
<div><%= f.label :email %><br />
<%= f.input :email, :autofocus => true %></div>
<div><%= f.submit "Update" %></div>
<% end %>
And there is my problem : when I try to modify the email address, nothing happen. The update fails.
How can I do this ?
I'm quite lost.
Thanks by advance.
Here's the log file :
Started PUT "/backend/users/1" for 127.0.0.1 at 2012-11-13 12:13:51 +0100
Processing by Backend::UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wWrUDh7LVWhP+P7OWO6laDWaCKInxk37AA2BPuQWAI4=", "backend_user"=>{"email"=>"grellazzi#laposte.net"}, "commit"=>"Update", "id"=>"1"}
[1m[35mBackend::User Load (0.0ms)[0m SELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = 1 LIMIT 1
[1m[36mBackend::User Load (0.0ms)[0m [1mSELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = ? LIMIT 1[0m [["id", "1"]]
[1m[35mSQL (1.0ms)[0m BEGIN
[1m[36m (0.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/backend/users/1
Completed 302 Found in 23ms (ActiveRecord: 1.0ms)
Started GET "/backend/users/1" for 127.0.0.1 at 2012-11-13 12:13:51 +0100
Processing by Backend::UsersController#show as HTML
Parameters: {"id"=>"1"}
[1m[35mBackend::User Load (0.0ms)[0m SELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = 1 LIMIT 1
[1m[36mBackend::User Load (0.0ms)[0m [1mSELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = ? LIMIT 1[0m [["id", "1"]]
Rendered backend/users/show.html.erb within layouts/admin (0.0ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 0.0ms)
Thanks for your links, I tried to modify my user_controller with
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
#user = User.find(current_user.id)
if #user.update_attributes(params[:user])
# Sign in the user bypassing validation in case his password changed
sign_in #user, :bypass => true
redirect_to root_path
else
render "edit"
end
But it fails...
If nothing happens, that usually indicates a validation error, in your case probably a missing password or other data that is not part of your form but is still being validated. To debug this, output #user.errors.inspect to your logfile or look at error_messages_on #user in your view.
In general, take a look at these guides on how to allow users to edit their (partial) profile:
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password
I am running functional tests with Test::Unit against my rails 3 application that uses Devise and CanCan to help manage users. However, I am hitting a weird issue in my requests when I make a POST or PUT to my contacts_controller. The request fails when the controller loads the User from the database, and then stops, which then results in a failed test.
Would anyone have an idea of why this is occurring?
Console Output
SQL (2.8ms) describe `roles_users`
SQL (2.1ms) describe `camps_users`
SQL (3.4ms) describe `camps_users`
SQL (2.0ms) describe `campers_camps`
SQL (2.8ms) describe `campers_camps`
SQL (2.1ms) describe `roles_users`
Loaded suite test/functional/camp/contacts_controller_test
Started
SQL (0.1ms) BEGIN
SQL (0.7ms) SHOW TABLES
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 292811013) LIMIT 1
SQL (0.4ms) SELECT COUNT(*) FROM `contacts`
Camp Load (0.3ms) SELECT `camps`.* FROM `camps` WHERE (`camps`.`id` = 665138414) LIMIT 1
Processing by Camp::ContactsController#create as HTML
Parameters: {"contact"=>{"first_name"=>"John", "last_name"=>"Doe", "email_address"=>"test#test.com", "phone_number"=>"1234567890"}, "camp_id"=>"bolo"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 292811013) LIMIT 1
Completed in 24ms
SQL (0.3ms) SELECT COUNT(*) FROM `contacts`
SQL (0.1ms) ROLLBACK
F
Finished in 0.385608 seconds.
1) Failure:
test_should_create_contact(Camp::ContactsControllerTest) [test/functional/camp/contacts_controller_test.rb:45]:
"Contact.count" didn't change by 1. <5> expected but was <4>.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
Test run options: --seed 10895 --name "test_should_create_contact"
Contact Test
test "should create contact" do
assert_difference('Contact.count') do
post :create,
:contact => {
:first_name => "John",
:last_name => "Doe",
:email_address => "test#test.com",
:phone_number => "1234567890"
},
:camp_id => camps(:bolo).uri
end
assert_response :created
assert_not_nil assigns(:contact)
get :show,
:id => assigns(:contact).to_param,
:camp_id => camps(:bolo).uri
assert_response :success
end
Contact Controller
class Camp::ContactsController < AuthorizedController
def create
#contact = #camp.contacts.build(params[:contact])
respond_to do |format|
if #contact.save
format.html { render :text => "contact created!", :status => :created }
format.xml {render :xml => #contact, :status => :created, :location => #contact}
else
format.html { render :action => "new", :status => :bad_request }
format.xml { render :xml => #contact.errors, :status => :bad_request }
end
end
end
end
Authorized Controller
class AuthorizedController < ApplicationController
before_filter :authenticate_user!
check_authorization
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
respond_to do |format|
format.html { redirect_to new_user_session_path, :status => :unauthorized }
format.xml { render :xml => "...", :status => :unauthorized }
end
end
end
CanCan Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role? :admin
can :manage, :all
elsif user.role? :account_admin
can [:show, :update, :destroy, :create], Camp do |camp|
user.is_linked_to_camp?(camp)
end
can :manage, Contact do |contact|
user.is_obj_linked_to_camp?(contact)
end
else
cannot :manage, :all
end
end
Turns out is was a bug in my code. My tests were not updated to account for a new schema in the Contacts model.