I have tried to push a simple rails 3.2.3 application with mongo_mapper to cloudfoundry, but I am getting 504 Gateway Time-out. The application ran fine on localhost. I provided additional information below. Please could you provide me assistance to resolve this issue?
To create and push application I typed:
rails new myapp -skip-active-record
scaffold massage content:string --skip-migration --orm mongo_mapper
bundle update
bundle install
bundle package
vmc push myapp --runtime ruby19
Here is the Gemfile:
Source 'http://rubygems.org'
gem rails', '3.2.3
gem mongo_mapper
gem bson_ext
end
Here is the controller:
class MessagesController < ApplicationController
def index
#messages = Message.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #messages }
end
end
def show
#message = Message.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #message }
end
end
def new
#message = Message.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #message }
end
end
def edit
#message = Message.find(params[:id])
end
def create
#message = Message.new(params[:message])
respond_to do |format|
if #message.save
format.html { redirect_to #message, notice: 'Message was successfully created.' }
format.json { render json: #message, status: :created, location: #message }
else
format.html { render action: "new" }
format.json { render json: #message.errors, status: :unprocessable_entity }
end
end
end
def update
#message = Message.find(params[:id])
respond_to do |format|
if #message.update_attributes(params[:message])
format.html { redirect_to #message, notice: 'Message was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #message.errors, status: :unprocessable_entity }
end
end
end
def destroy
#message = Message.find(params[:id])
#message.destroy
respond_to do |format|
format.html { redirect_to messages_url }
format.json { head :no_content }
end
end
end
Here is the modle:
class Message
include MongoMapper::Document
key :content, String
end
Here is the index view
<h1>Listing messages</h1>
<table>
<tr>
<th>Content</th>
</tr>
<% #messages.each do |message| %>
<tr>
<td><%= message.content %></td>
<td><%= link_to 'Show', message %></td>
<td><%= link_to 'Edit', edit_message_path(message) %></td>
<td><%= link_to 'Destroy', message, :confirm => 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Message', new_message_path %>
Here is config/locales/mongo.yml
defaults: &defaults
host: 127.0.0.1
port: 27017
development:
<<: *defaults
database: proj815_development
test:
<<: *defaults
database: proj815_test
# set these environment variables on your prod server
production:
host: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['hostname'] rescue 'localhost'%>
port: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['port'] rescue '27017'%>
database: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['db'] rescue 'proj815' %>
username: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['username'] rescue ''%>
password: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['password'] rescue ''%>
Normally this happens because your application is too large to upload, if you can reduce the size of your application, you should be fine.
Related
i have a method which i wan to call from my controller. What i've read in stacks are most of them said to include the method in applicationController.Therefore i did. But still i got the following error. Can some1 help me ? New to rails
APPLICATION CONTROLLER :
helper_method :random_generate
def random_generate
length = #generator.primer_length
chars = 'ATGC'
seq = ''
length.times { seq << chars[rand(chars.size)] }
#generator.random_primer_generated= seq
end
CONTROLLER :
class GeneratorsController < ApplicationController
before_action :set_generator, only: [:show, :edit, :update, :destroy]
after_action :generate_option, only: [:create,:edit]
helper_method :random_primer_generated
# GET /generators
# GET /generators.json
def index
#generators = Generator.all
end
# GET /generators/1
# GET /generators/1.json
def show
end
# GET /generators/new
def new
#generator = Generator.new
end
# GET /generators/1/edit
def edit
end
# POST /generators
# POST /generators.json
def create
#generator = Generator.new(generator_params)
respond_to do |format|
if #generator.save
format.html { redirect_to #generator, notice: 'Generator was successfully created.' }
format.json { render action: 'show', status: :created, location: #generator }
else
format.html { render action: 'new' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(generator_params)
format.html { redirect_to #generator, notice: 'Generator was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /generators/1
# DELETE /generators/1.json
def destroy
#generator.destroy
respond_to do |format|
format.html { redirect_to generators_url }
format.json { head :no_content }
end
end
def generate_option
if params[:choice] == 'Randomly'
#generator.random_generate
end
#generator.save!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_generator
#generator = Generator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def generator_params
params.require(:generator).permit(:primer_length, :choice, :random_primer_generated)
end
end
VIEW:
<%= form_for (#generator ) do |f| %>
<% if #generator.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#generator.errors.count, "error") %> prohibited this generator from being saved:</h2>
<ul>
<% #generator.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<label>Primer Length</label><br>
<%= f.number_field :primer_length %>
</div>
<label>Selection :</label><br>
<label>Randomly</label>
<%= radio_button_tag(:choice, 'Randomly')%>
<%= button_to('Generate',generate_option_generator_path(#generator))%>
<% end %>
If you want to call random_generate on the #generator, you have to implement that method in the Generator class. What you have done is implemented it in the ApplicationController, so you would have to call it without referencing to #generator.
Also, I think calling helper_method :random_generate is only required if you want to call random_generate as a controller method inside of a view.
I am currently following the Agile Web Development book (Rails 3) and have been stuck with a Private Method error. After hours of research I have not been able to find a solution.
The following is the code I'm working with and have discovered the string giving the problem is:
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
There is a similar problem/solution that has been posted where the solution was to place the line Item class above the private, however, the line item class is not private.
Any help would be appreciated.
CODE
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<!-- START_HIGHLIGHT -->
<h2>Your Cart</h2>
<table>
<!-- END_HIGHLIGHT -->
<% #cart.line_items.each do |item| %>
<!-- START_HIGHLIGHT -->
<tr>
<td><%= item.quantity %>×</td>
<td><%= item.product.title %></td>
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
</tr>
<!-- END_HIGHLIGHT -->
<% end %>
<!-- START_HIGHLIGHT -->
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(#cart.total_price) %></td>
</tr>
<!-- END_HIGHLIGHT -->
<!-- START_HIGHLIGHT -->
</table>
<!-- END_HIGHLIGHT -->
<%= button_to 'Empty cart', #cart, method: :delete,
data: { confirm: 'Are you sure?' } %>
Error
private method `total_price' called for #<LineItem
line_item model
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
attr_accessible :cart_id, :product_id
end
def total_price
product.price * quantity
end
cart model
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
end
line item controller
class LineItemsController < ApplicationController
# GET /line_items
# GET /line_items.json
def index
#line_items = LineItem.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #line_items }
end
end
# GET /line_items/1
# GET /line_items/1.json
def show
#line_item = LineItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #line_item }
end
end
# GET /line_items/new
# GET /line_items/new.json
def new
#line_item = LineItem.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #line_item }
end
end
# GET /line_items/1/edit
def edit
#line_item = LineItem.find(params[:id])
end
# POST /line_items
# POST /line_items.json
def create
#cart = current_cart
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product.id)
#line_item.product = product
respond_to do |format|
if #line_item.save
format.html { redirect_to #line_item.cart,
notice: 'Line item was successfully created.' }
format.json { render json: #line_item,
status: :created, location: #line_item }
else
format.html { render action: "new" }
format.json { render json: #line_item.errors,
status: :unprocessable_entity }
end
end
end
# PUT /line_items/1
# PUT /line_items/1.json
def update
#line_item = LineItem.find(params[:id])
respond_to do |format|
if #line_item.update_attributes(params[:line_item])
format.html { redirect_to #line_item, notice: 'Line item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
#line_item = LineItem.find(params[:id])
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.json { head :no_content }
end
end
end
In your LineItem model total_price is defined outside of the LineItem class. So it should be:
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
attr_accessible :cart_id, :product_id
def total_price
product.price * quantity
end
end
You are saying that #cart.total_price is giving the error but your error showing that it is looking this function in LineItem model. It probably means you must have a total_price method in LineItem model also. Do you have that? And yes as others said, it would be better to add your controller and model codes in your post.
Edit:
Based on your update in the post, yes the total_price method should be inside in your class. does that solve the problem?
i update class_name
/models/tipotercero.rb
class Tipotercero < ActiveRecord::Base
has_many :terceroclasificaciones
has_many :terceros , :class_name => "Terceroclasificacion"
end
/models/tercero.rb
class Tercero < ActiveRecord::Base
has_many :ciudades
has_many :terceroclasificaciones
has_many :tipoterceros, :class_name => "Terceroclasificacion"
end
class Terceroclasificacion < ActiveRecord::Base
belongs_to :tercero
belongs_to :tipotercero
attr_accessor :tercero_id, :tipotercero_id
end
/views/terceros/_form.html.erb
<div class="field">
<% for tipotercero in Tipotercero.all %>
<div>
<%= check_box_tag "tercero[tipotercero_ids][]", tipotercero.id, #tercero.tipoterceros.include?(tipotercero) %>
<%= tipotercero.nombre %>
</div>
<% end %>
</div>
the error is
uninitialized constant Tercero::terceroclasificacion
I have tried to follow the post
Rails 3 has_many :through Form but I could not find the error in my application
add terceros controller
/controllers/terceros_controller.rb
class TercerosController < ApplicationController
# GET /terceros
# GET /terceros.xml
def index
#terceros = Tercero.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #terceros }
end
end
# GET /terceros/1
# GET /terceros/1.xml
def show
#tercero = Tercero.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #tercero }
end
end
# GET /terceros/new
# GET /terceros/new.xml
def new
#tercero = Tercero.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #tercero }
end
end
# GET /terceros/1/edit
def edit
#tercero = Tercero.find(params[:id])
end
# POST /terceros
# POST /terceros.xml
def create
#tercero = Tercero.new(params[:tercero])
# #tercero.attributes = {'tipotercero_ids' => []}.merge(params[:tercero] || {})
respond_to do |format|
if #tercero.save
format.html { redirect_to(#tercero, :notice => 'Tercero was successfully created.') }
format.xml { render :xml => #tercero, :status => :created, :location => #tercero }
else
format.html { render :action => "new" }
format.xml { render :xml => #tercero.errors, :status => :unprocessable_entity }
end
end
end
# PUT /terceros/1
# PUT /terceros/1.xml
def update
#tercero = Tercero.find(params[:id])
respond_to do |format|
if #tercero.update_attributes(params[:tercero])
format.html { redirect_to(#tercero, :notice => 'Tercero was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #tercero.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /terceros/1
# DELETE /terceros/1.xml
def destroy
#tercero = Tercero.find(params[:id])
#tercero.destroy
respond_to do |format|
format.html { redirect_to(terceros_url) }
format.xml { head :ok }
end
end
end
edit 3
the page error is:
NameError in Terceros#new
Showing /home/andres/desarrollos/rubyonrails/proyecto/app/views/terceros/_form.html.erb where line #92 raised:
uninitialized constant Tercero::terceroclasificacion
Extracted source (around line #92):
89: <div class="field">
90: <% for tipotercero in Tipotercero.all %>
91: <div>
92: <%= check_box_tag "tercero[tipotercero_ids][]", tipotercero.id, #tercero.tipoterceros.include?(tipotercero) %>
93: <%= tipotercero.nombre %>
94: </div>
95: <% end %>
Trace of template inclusion: app/views/terceros/new.html.erb
4 edit
error to create tercero is:
NoMethodError in TercerosController#create
undefined method `type_cast' for nil:NilClass
Rails.root: /home/andres/desarrollos/rubyonrails/proyecto
Application Trace | Framework Trace | Full Trace
app/controllers/terceros_controller.rb:44:in `new'
app/controllers/terceros_controller.rb:44:in `create'
Request
Parameters:
{"tercero"=>{"identificacion"=>"1110465574",
"empresa"=>"hogar",
"tipo_identificacion"=>"2",
"direccion1"=>"dierccion",
"nombre"=>"carlos andres",
"direccion2"=>"123",
"ciudad_id"=>"2",
"telefono_fijo"=>"132233",
"telefono_movil"=>"123123",
"fecha_nacimiento(1i)"=>"2013",
"observaciones"=>"",
"fecha_nacimiento(2i)"=>"10",
"fecha_nacimiento(3i)"=>"17",
"representante_legal"=>"",
"tipotercero_ids"=>["1",
"2",
"3"],
"apellido1"=>"colonia",
"apellido2"=>"riveros",
"pagina_web"=>""},
"commit"=>"Crear Tercero",
"authenticity_token"=>"SeUoILctpNr9t6Lx8wSoHVTO5mjk0qJfnzJsb9Jtzao=",
"utf8"=>"✓"}
Sometimes other model cannot be derived from the association name just by Rails conventions and such strange errors occurs. You can pass class name like this:
has_many :terceroclasificaciones, :class_name => "Terceroclasificacion"
And btw, you can make your life easier just by using English names for your models and stuff. You do localization in your view anyways.
Try to remove this
attr_accessor :tercero_id, :tipotercero_id
I think you want it to be an attr_accessible but not attr_accessor
I'm quite new to Rails and to this Q&A website! So hello everyone, I'm a French Rails tester. I have cross posted this question to Railsforum too, to have a better chance on getting an answer :) (http://railsforum.com/viewtopic.php?id=44238)
I need help on something. It seems simple, but in my opinion, not well documented (I browsed the web for 3 days without finding a clean answer. Some partial solutions here, but not a complete and clean one for a newbie!)
I want to add some actions to a default scaffolded controller to handle creation and modification of multiple records in the same form.
rails generate scaffold Item title:string
This is the sample scaffolded code:
app/controllers/items_controller.rb
class ItemsController < ApplicationController
# GET /items
# GET /items.xml
def index
#items = Item.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #items }
end
end
# GET /items/1
# GET /items/1.xml
def show
#item = Item.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #item }
end
end
# GET /items/new
# GET /items/new.xml
def new
#item = Item.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #item }
end
end
# GET /items/1/edit
def edit
#item = Item.find(params[:id])
end
# POST /items
# POST /items.xml
def create
#item = Item.new(params[:item])
respond_to do |format|
if #item.save
format.html { redirect_to(#item, :notice => 'Item was successfully created.') }
format.xml { render :xml => #item, :status => :created, :location => #item }
else
format.html { render :action => "new" }
format.xml { render :xml => #item.errors, :status => :unprocessable_entity }
end
end
end
# PUT /items/1
# PUT /items/1.xml
def update
#item = Item.find(params[:id])
respond_to do |format|
if #item.update_attributes(params[:item])
format.html { redirect_to(#item, :notice => 'Item was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #item.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.xml
def destroy
#item = Item.find(params[:id])
#item.destroy
respond_to do |format|
format.html { redirect_to(items_url) }
format.xml { head :ok }
end
end
end
app/views/items/new.html.erb
<h1>New item</h1>
<%= render 'form' %>
<%= link_to 'Back', items_path %>
app/views/items/edit.html.erb
<h1>Editing item</h1>
<%= render 'form' %>
<%= link_to 'Show', #item %> |
<%= link_to 'Back', items_path %>
app/views/items/_form.html.erb
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In my opinion, I need to add 4 methods to my Item controller:
new_multiple : to initialize the new
multiple form view
create_multiple :
to handle results from the new
multiple form view
edit_mulitple : to
initialize the edit multiple form
view
update_mulitple : to handle
results from the edit multiple form
view
Then, I need to create 2 new views:
new_multiple.html.erb with code for a multiple record (5 in the same time) creation form
edit_mulitple.html.erb with code for a multiple record (5 in the same time) edition form
and maybe _partials to avoid DRY stuff...
Anyone want to help me write these 4 actions and 2 views? I would really appreciate that.
Thanks! :)
Note : In my application items are children of parentItems but I DON'T WANT to use nested_form to create items in parentItems forms like in railscast 196 and 197.
HI Guys. I wanna know how can I make a simple search using named scope in rails 3. I have successfully done it in the console but I can't find any example that uses the views.
Here is the code for the model trap.rb:
class Trap < ActiveRecord::Base
scope :by_date_entry, lambda { |arg| where(["traps.date_entry = ?",arg])}
scope :by_empcode, lambda { |arg| where(["traps.empcode = ?",arg])}
end
In the controller traps_controller.rb:
class TrapsController < ApplicationController
# GET /traps
# GET /traps.xml
def index
#traps = Trap.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
# GET /traps/1
# GET /traps/1.xml
def show
#trap = Trap.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #trap }
end
end
# GET /traps/new
# GET /traps/new.xml
def new
#trap = Trap.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #trap }
end
end
# GET /traps/1/edit
def edit
#trap = Trap.find(params[:id])
end
# POST /traps
# POST /traps.xml
def create
#trap = Trap.new(params[:trap])
respond_to do |format|
if #trap.save
format.html { redirect_to(#trap, :notice => 'Trap was successfully created.') }
format.xml { render :xml => #trap, :status => :created, :location => #trap }
else
format.html { render :action => "new" }
format.xml { render :xml => #trap.errors, :status => :unprocessable_entity }
end
end
end
# PUT /traps/1
# PUT /traps/1.xml
def update
#trap = Trap.find(params[:id])
respond_to do |format|
if #trap.update_attributes(params[:trap])
format.html { redirect_to(#trap, :notice => 'Trap was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #trap.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /traps/1
# DELETE /traps/1.xml
def destroy
#trap = Trap.find(params[:id])
#trap.destroy
respond_to do |format|
format.html { redirect_to(traps_url) }
format.xml { head :ok }
end
end
end
And in my view index.html.erb:
<h2>Time Reconciliation Application for Payroll 1.0</h2>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
I wanna put like a search text box in which the user can input the employee_code or the date_entry in the index.html.erb. I have done something like this using meta_search and meta_where but I would prefer to use the named_scope but I don't know how to display this using view and controller. The codes in my model trap.rb is working on my console. I just don't know how to make it appear in the view. Pls help...
I would probably do something like this in the controller:
def index
# the scoped method returns a prepared database call with
# no arguments so the database is not called yet.
#traps = Trap.scoped
# if any parameter to filter is supplied then use the scope
#traps = #traps.by_date_entry(params[:date_entry]) if params[:date_entry]
#traps = #traps.by_empcode(params[:empcode]) if params[:empcode]
end
And then in the view, you could either go with a form to send the parameters, or if you just want to try this then create a link for the specific dates or empcodes like this:
<td><%= link_to trap.date_entry, traps_path(:date_entry => trap.date_entry) %></td>
So that when you click the link it will send the date_entry to be used by your scope. By the way, the traps_path is of course only valid as long as you specified resources :traps in your routes.rb