I'm building a marketplace app where I'm trying to use the Best in Place gem to allow sellers to add a tracking number for each of their orders.
I get a NoMethodError which I'm not able to resolve.
NoMethodError in Orders#sales
undefined method `tracking' for nil:NilClass
The error points to the best in place line below in the view page. This view page is based on the method Sales (in the controller below) where I filter for orders for that particular seller.
Here is my routes.rb with order routing. Since orders need not be edited or destroyed, I didn't create an edit or delete route.
resources :listings do
resources :orders, only: [:new, :create, :update]
collection { post :import }
end
Here is a snippet from my orders controller
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :check_user, only: [:edit, :update]
def sales
#orders = Order.all.where(seller: current_user).order("created_at DESC")
end
def update
#order.update_attributes(params[:order])
end
def check_user
if current_user.id != #seller && current_user.name != "admin admin"
redirect_to root_url, alert: "Sorry, you are not the seller of this listing"
end
end
Here is my view page:
<table class="table table-striped table-bordered">
<tr>
<th class="col-md-2">Image</th>
<th class="col-md-2">Item</th>
<th class="col-md-1">Price</th>
<th class="col-md-2">Customer</th>
<th class="col-md-2">Date Sold</th>
<th class="col-md-2">Shipment Tracking #</th>
<th class="col-md-1">Carrier (UPS, USPS, etc.)</th>
</tr>
<% #orders.each do |order| %>
<tr>
<td><%= image_tag order.listing.image.url(:thumb) %></td>
<td><%= order.listing.name %></td>
<td><%= number_to_currency(order.listing.price) %></td>
<td><%= order.buyer.name %></td>
<td><%= order.created_at.strftime("%B %-d, %Y") %></td>
<td><%= best_in_place #order, :tracking, :type => :input %> </td>
<td><%= best_in_place #order, :carrier, :type => :input %></td>
</tr>
<% end %>
</table>
Been stuck on this for a while. Appreciate any help on this.
I think the problem is that you are calling #order inside your .each method.
Try:
<%= best in place order, :tracking, :type => :input %>
You will need to change the next line in your view as well.
I figured it out. The problem was that since I was using best_in_place in a non-Activerecord environment (as part of a table with a list of orders), I needed to pass the order id explicitly. I found this in the best_in_place documentation https://github.com/bernat/best_in_place#non-active-record-environments
I created a custom route for the update action
put 'orderupdate' => "orders#update"
Then in my do loop in the view, I used the custom path for the route above and passed the order id to that route.
<% #orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<td><%= image_tag order.listing.image.url(:thumb) %></td>
<td><%= order.listing.name %></td>
<td><%= number_to_currency(order.listing.price) %></td>
<td><%= order.buyer.name %></td>
<td><%= order.created_at.strftime("%B %-d, %Y") %></td>
<td><%= best_in_place order, :tracking, :type => :input, :url => orderupdate_path(id: order.id) %> </td>
<td><%= best_in_place order, :carrier, :type => :input, :url => orderupdate_path(id: order.id) %> </td>
</tr>
<% end %>
Here is the update method in my controller:
def update
#order = Order.find(params[:id])
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to sales_url, notice: 'Order updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
Hope this helps someone!
Related
so i have installed best in place gem in a rails 4 environment and initialised it correctly. (i can click on the name field and the box becomes editable).
I've this code in my admin_namespaced user controller
class Admin::UsersController < Admin::BaseController
def index
#users = User.all
# #columns = User.column_names
end
def show
#user = User.find(params[:id])
end
def update
#user = User.find params[:id]
respond_to do |format|
if #user.update_attributes(user_params)
format.html { redirect_to(user, :notice => 'User was successfully updated.') }
format.json { respond_with_bip(#user) }
else
format.html { render :action => "index" }
format.json { respond_with_bip(#user) }
end
end
end
private
def user_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
end
and basically i want to use it in conjuction with rails datatables gem that i successfully setup, to inline-edit corresponding fields.
this is the html.erb code in my user index view
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= link_to "Back", admin_path %>
<table class="display responsive no-wrap text-center" id="usertableadmin">
<thead>
<tr>
<th>id</th>
<th>Username</th>
<th>Email</th>
<th>Activated?</th>
<th>Admin?</th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= best_in_place user, :name%></td>
<td><%= user.email %></td>
<td><%= user.activated %></td>
<td><%= user.admin %></td>
</tr>
<% end %>
</tbody>
</table>
here is what the html code looks like on the tag that has the best_in_place initialization.
<span data-bip-type="input" data-bip-attribute="name" data-bip-object="user" data-bip-original-content="Example User" data-bip-url="/users/1" data-bip-value="Example User" class="best_in_place" id="best_in_place_user_1_name">Example User</span>
I dont know for sure but for some reason the fields do not get updated. When i click to change the name it gets reverted to the previous one.
I dont know if its because i have a namespace, admin/users or its because its the index action and not the show action.
any insight is welcome.
I've found the solution,
it seems the error was the update url it was wrong because of the namespace.
What i had to do, was to include url parameter like below
<td><%= best_in_place user, :name, url: "users/#{user.id}" %></td>
I'm trying to call localhost:3000/connections so I can see if I have set up connections correctly in my app.
When I do, I receive an error message saying:
ActiveRecord::StatementInvalid in Connections#index
Showing c:/Users/Doesha/desktop/connections/app/views/connections/index.html.erb where line #14 raised:
SQLite3::SQLException: no such table: connections: SELECT "connections".* FROM "connections"
Here's my index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Connections</h1>
<table>
<thead>
<tr>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #connections.each do |connection| %>
<tr>
<td><%= connection.description %></td>
<td><%= link_to 'Show', connection %></td>
<td><%= link_to 'Edit', edit_connection_path(connection) %></td>
<td><%= link_to 'Destroy', connection, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Connection', new_connection_path %>
Here's my connections_controller.rb:
class ConnectionsController < ApplicationController
before_action :set_connection, only: [:show, :edit, :update, :destroy]
def index
#connections = Connection.all
end
def show
end
def new
#connection = Connect.new
end
def edit
end
def create
#connection = Connection.new(connection_params)
if #connection.save
redirect_to #connection, notice: 'Connection was successfully created.'
else
render action :new
end
end
def update
if #connection.update(connection_params)
redirect_to #connection, notice: 'Connection was successfully updated.'
else
render action :edit
end
end
def destroy
#connection.destroy
redirect_to connections_url, notice: 'Connection was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_connection
#connection = Connection.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def connection_params
params.require(:connection).permit(:description)
end
end
Anyone know what the problem might be? Thanks!
The SQLite error message is saying that no connections table exists.
You probably need to run rake db:migrate in the terminal window to create the necessary table. After you restart the server, the action should work fine.
What I Want:
I need in a view a button or a link (it doesn't matter) to the create action of Reservation controller and to give it a parameter too.
And resolve the ForbiddenAttributesError that now gives me.
Here are my model and controller:
Reservation model
class Reservation < ActiveRecord::Base
belongs_to :user
belongs_to :dinner
end
Reservation controller
class ReservationsController < ApplicationController
load_and_authorize_resource
def show
#reservations = Reservation.joins(:user).where('dinner_id' => params[:dinner_id]).select("users.*,reservations.*")
#dinnerid = params[:dinner_id]
respond_to do |format|
format.html
format.json { render :json => #reservations }
end
end
def create
#reservation = Reservation.new(params[:reservation])
#reservation.user_id = current_user.id
respond_to do |format|
if #reservation.save
format.html { redirect_to #reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :show }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:dinner_id)
end
end
EDIT: After the suggestion of #Rahul Singh this is my actual code with relative error:
<p id="notice"><%= notice %></p>
<table>
<thead>
<tr>
<th>Id</th>
<th>User id</th>
<th>Dinner id</th>
<th>User email</th>
<th>User name</th>
</tr>
</thead>
<tbody>
<% #reservations.each do |reservation| %>
<tr>
<td><%= reservation.id %></td>
<td><%= reservation.user_id %></td>
<td><%= reservation.dinner_id %></td>
<td><%= reservation.user.email %></td>
<td><%= reservation.user.name %></td>
</tr>
<% end %>
</tbody>
</table>
<br/>TRY 00a <br/>
<%= form_for(Reservation.new) do |f| %>
<%= f.hidden_field( :dinner_id, :value => #dinnerid.to_s) %>
<%= f.submit "Join1" %>
<% end %>
<br/> !!!!!!!!!!ERROR : ActiveModel::ForbiddenAttributesError
<br/>TRY 00b <br/>
<%= link_to "Join1", reservations_path(dinner_id:#dinnerid.to_s), method: :post %>
<br/> !!!!!!!!!!ERROR : param is missing or the value is empty: reservation
I provide a sreenshot for the error :
Error of the form : https://www.dropbox.com/s/i2x1m520ptqdj56/createReservationForm.jpg
Error of the link_to : https://www.dropbox.com/s/8xjwee5oo7q6uhk/createReservationLink_to.jpg
This should work
<%= form_for(Reservation.new) do |f| %>
<%= f.hidden_field( :dinner_id, :value => #dinnerid.to_s) %>
<%= f.submit "Join1" %>
<% end %>
clicking on Join1 button will submit form to ReservationsController create action.
and with link try this
<%= link_to "Join1", reservations_path(dinner_id:#dinnerid.to_s), method: :post %>
for above to work,add following in your routes.rb
resources :reservations
Change this line -> #reservation = Reservation.new(params[:reservation])
To this -> #reservation = Reservation.new reservation_params
and try again ;).
I'm wondering if it wouldn't be better to have your reservation routes as a nested resource of dinners.
It seems reservations can't exist without a dinner, so I'd make that explicit like this:
# config/routes.rb
resources :dinners do
resources :reservations
end
Run rake routes to see how this would change the routes.
You'd now have the dinner id passed along:
# app/views/dinners/show.html.erb
<%= button_to 'Reserve this dinner', dinner_reservations_path(#dinner) %>
The button would route to the create action because a button's default HTTP method is POST.
# app/controllers/reservations_controller.rb
class ReservationsController < ApplicationController
before_action :set_dinner
def create
#dinner.reservations.create! user: current_user
# would render reservations/create.html.erb
end
private
def set_dinner
#dinner = Dinner.find(params[:id])
end
end
This doesn't fix your immediate problem of just getting that link to work. But I think you'd be a lot better served structuring your app more like the above going forward.
Full disclosure: the person who asked this question contacted me on twitter personally, so I took some liberties in answering this question with a more general design suggestion.
I'm not sure this is the "best" approach but I think its the easiest one.
You could do something with string interpolation:
a href="/reservations?dinner=#{dinner.id}" Join
then you could get the paramter with
params[:dinner]
I am hoping someone can help me. I am getting the following issue:
No route matches {:action=>"show", :controller=>"stocks", :stockpile_id=>#<Stock id: 17, stockpile_id: 3, code: "rttrtrt", name: "", description: "", quantity: nil, cost_pence: nil, information: "", created_at: "2013-08-18 19:52:46", updated_at: "2013-08-18 19:52:46">, :id=>nil, :format=>nil} missing required keys: [:id]
When visiting the following URL: /admin/stockpiles/3/stocks/
My routes look like:
scope '/admin' do
root :to => 'admin#index', :as => 'admin'
resources :stockpiles,:companies
scope :path => 'stockpiles/:stockpile_id' do
resources :stocks
end
end
The data contained in the error message:
id | stockpile_id | code | name | description | quantity | cost_pence | information | created_at | updated_at
17 | 3 | rttrtrt | | | | | | 2013-08-18 19:52:46.856864 | 2013-08-18 19:52:46.856864
My Stock Model:
class Stock < ActiveRecord::Base
belongs_to :stockpile
end
Nothing interesting in the Stockpile model, just that it has many stocks..
Here is my controller:
class StocksController < ApplicationController
before_action :set_stock, only: [:show, :edit, :update, :destroy]
def index
#stockpile = Stockpile.find(params[:stockpile_id])
#stocks = #stockpile.stocks.all
end
def show
end
def new
#stockpile = Stockpile.find(params[:stockpile_id])
#stock = #stockpile.stocks.new
end
def edit
end
def create
#stockpile = Stockpile.find(params[:stockpile_id])
#stock = #stockpile.stocks.new(stock_params)
if #stock.save
redirect_to #stock, notice: 'Stock was successfully created.'
else
render action: 'new'
end
end
def update
if #stock.update(stock_params)
redirect_to #stock, notice: 'Stock was successfully updated.'
else
render action: 'edit'
end
end
def destroy
#stock.destroy
redirect_to stocks_url, notice: 'Stock was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_stock
#stockpile = Stockpile.find(params[:stockpile_id])
#stock = #stockpile.stocks.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def stock_params
params.require(:stock).permit(:code, :name, :description, :quantity, :cost_pence, :information)
end
end
And here is the view in question:
<div class="page-header">
<%= link_to new_stock_path(params[:stockpile_id]), :class => 'btn btn-primary' do %>
<i class="icon-plus icon-white"></i>
New Stock
<% end %>
<h1>Listing stocks</h1>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Stockpile</th>
<th>Code</th>
<th>Name</th>
<th>Description</th>
<th>Quantity</th>
<th>Cost pence</th>
<th>Information</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #stocks.each do |stock| %>
<tr>
<td><%= stock.stockpile %></td>
<td><%= stock.code %></td>
<td><%= stock.name %></td>
<td><%= stock.description %></td>
<td><%= stock.quantity %></td>
<td><%= stock.cost_pence %></td>
<td><%= stock.information %></td>
<td><%= link_to 'Show', stock %></td>
<td><%= link_to 'Edit', edit_stock_path(stock) %></td>
<td><%= link_to 'Destroy', stock, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
Help would be much appreciated - looks like something weird is going on because stockpile_id seems to be set to stock, and there seems to be no stock params or anything - so we get the missing id error.
Thanks.
It would appear that you need to pass in the stockpile as well as the stock..
<td><%= link_to 'Show', [#stockpile, stock] %></td>
Should do that.
As opposed to using the scope you can probably just use a nested resource in your routes
resources :stockpiles do
resources :stocks
end
and then you can DRY up your stock controller
class StocksController < ApplicationController
before_action :set_stockpile
.....
def set_stockpile
#stockpile = Stockpile.find params[:stockpile_id]
end
I have two models Jobs and Questions. A job has many questions and questions belong to a job.
I've set up the resources in the model, as well as the routes. I am having an issue trying to link_to the Show method of the questions controller on the questions#index page. My rake routes say that the path should be job_question_path with the two necessary :id's being :job_id and :id , so I tried:
<td><%= link_to 'Show', job_question_path(#job, question) %></td>
and got the error:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil, :id=>#<Question id: 1, job_id: 1, question1: "sfsdfssfs", question2: "sfsdfs", question3: "sfsdf", question4: "sfsdfsf", question5: "sfsfsfs", created_at: "2011-06-21 03:25:12", updated_at: "2011-06-21 03:25:12">}
I've tried multiple other combos and nothing is seeming to work, I keep getting:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil }
or some combination of that.
The part I don't get is that I can put in the url /jobs/1/questions/1 and it takes me to the show page, so I am assuming that my questions#show methods are ok. See below for the rest of my code.
Questions#index view
<% #questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<td><%= link_to 'Show', job_question_path(#job, question) %></td>
</tr>
<% end %>
Questions Controller
def index
#questions = Question.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #questions }
end
end
def show
#job = Job.find(params[:job_id])
#question = #job.questions.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #question }
end
end
Models
class Job < ActiveRecord::Base
has_many :questions
class Question < ActiveRecord::Base
belongs_to :job
Routes.rb
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
See this https://gist.github.com/1032734 for my rake routes.
Thanks for any help in advance, i've been at this for a while now and just can't figure out the solution. Please let me know if you need any more info.
may be so?
Questions#index view
<% #questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<%= link_to 'Show', job_question_path(question.job_id, question.id) %>
</tr>
It have to work. Or haven't you 'job_id' field in Questions table ?