我有一个适用于localhost
的应用程序但是当我部署到Heroku时,我的一个模型index
页面显示错误。有问题的模型是workouts#index
,这是我的Heroku日志显示错误:
2016-06-06T19:13:56.408259+00:00 app[web.1]: Started GET "/workouts" for 70.181.88.50 at 2016-06-06 19:13:56 +0000
2016-06-06T19:13:56.412002+00:00 app[web.1]: Processing by WorkoutsController#index as HTML
2016-06-06T19:13:56.417923+00:00 app[web.1]: Workout Load (4.8ms) SELECT "workouts".* FROM "workouts" ORDER BY created_at DESC LIMIT 1
2016-06-06T19:13:56.438116+00:00 app[web.1]:
2016-06-06T19:13:56.438123+00:00 app[web.1]: ActionView::Template::Error (No route matches {:action=>"show", :controller=>"workouts", :id=>nil} missing required keys: [:id]):
2016-06-06T19:13:56.438124+00:00 app[web.1]: 12: <%# if current_user != nil && current_user.admin? %>
2016-06-06T19:13:56.438125+00:00 app[web.1]: 13: <h3 class="cta"><%= link_to 'Create a New Workout', new_workout_path %></h2>
2016-06-06T19:13:56.438126+00:00 app[web.1]: 14: <%# else %>
2016-06-06T19:13:56.425828+00:00 app[web.1]: Rendered workouts/index.html.erb within layouts/application (2.1ms)
2016-06-06T19:13:56.434933+00:00 app[web.1]: Completed 500 Internal Server Error in 22ms (ActiveRecord: 4.8ms)
2016-06-06T19:13:56.438127+00:00 app[web.1]: 15: <h3 class="cta"><%= link_to 'Try the Most Recent Workout', workout_path(@most_recent_workout) %></h3>
2016-06-06T19:13:56.269357+00:00 app[web.1]: ActionView::Template::Error (No route matches {:action=>"show", :controller=>"workouts", :id=>nil} missing required keys: [:id]):
2016-06-06T19:13:56.269358+00:00 app[web.1]: 12: <%# if current_user != nil && current_user.admin? %>
2016-06-06T19:13:56.269360+00:00 app[web.1]: 13: <h3 class="cta"><%= link_to 'Create a New Workout', new_workout_path %></h2>
2016-06-06T19:13:56.269360+00:00 app[web.1]: 14: <%# else %>
2016-06-06T19:13:56.269362+00:00 app[web.1]: 15: <h3 class="cta"><%= link_to 'Try the Most Recent Workout', workout_path(@most_recent_workout) %></h3>
2016-06-06T19:13:56.269362+00:00 app[web.1]: 16: <%# end %>
2016-06-06T19:13:56.438128+00:00 app[web.1]: 16: <%# end %>
2016-06-06T19:13:56.438129+00:00 app[web.1]: 17: </div>
2016-06-06T19:13:56.438129+00:00 app[web.1]: 18:
2016-06-06T19:13:56.438130+00:00 app[web.1]: app/views/workouts/index.html.erb:15:in `_app_views_workouts_index_html_erb___1546867497932887864_70318699165220'
2016-06-06T19:13:56.438131+00:00 app[web.1]:
2016-06-06T19:13:56.438131+00:00 app[web.1]:
2016-06-06T19:13:56.450459+00:00 heroku[router]: at=info method=GET path="/workouts" host=radiant-island-49597.herokuapp.com request_id=f67b8f74-822c-47d4-84fa-6b95536e629d fwd="70.181.88.50" dyno=web.1 connect=1ms service=65ms status=500 bytes=1754
以下是workouts_controller
文件:
class WorkoutsController < ApplicationController
def index
@workouts = Workout.all.order("created_at DESC")
@most_recent_workout = Workout.order("created_at").last
end
def show
@workout = Workout.friendly.find(params[:id])
@exercise = Exercise.new
@report = Report.new
end
def new
@workout = Workout.new
@workout.user_id = current_user
end
def create
@workout = Workout.new(workout_params)
@workout.user = current_user
if @workout.save
flash[:notice] = "Workout was saved successfully."
redirect_to @workout
else
flash.now[:alert] = "Error creating workout. Please try again."
render :new
end
end
def edit
@workout = Workout.friendly.find(params[:id])
@workout.user_id = current_user
end
def update
@workout = Workout.friendly.find(params[:id])
@workout.name = params[:workout][:name]
@workout.workout_type = params[:workout][:workout_type]
@workout.teaser = params[:workout][:teaser]
@workout.description = params[:workout][:description]
@workout.video = params[:workout][:video]
@workout.difficulty = params[:workout][:difficulty]
@workout.trainer = params[:workout][:trainer]
@workout.user = current_user
if @workout.save
flash[:notice] = "Workout was updated successfully."
redirect_to @workout
else
flash.now[:alert] = "Error saving workout. Please try again."
render :edit
end
end
def destroy
@workout = Workout.friendly.find(params[:id])
if @workout.destroy
flash[:notice] = "\"#{@workout.name}\" was deleted successfully."
redirect_to action: :index
else
flash.now[:alert] = "There was an error deleting the workout."
render :show
end
end
private
def workout_params
params.require(:workout).permit(:name, :workout_type, :teaser, :description, :video, :difficulty, :trainer, :slug, :user_id)
end
end
以下是show.html.erb
文件的代码:
<div class="hero-image-row">
<div class="hero-image-outer text-center">
<div class="hero-image-inner text-center">
<%= image_tag 'bg-r-tirepushup.jpg', class: "hero-image",alt: "girl doing pushups on a stack of tires" %>
</div> <!-- hero-image-inner -->
</div> <!-- hero-image-inner -->
</div> <!-- row -->
<div class="container overlap-hero-image col-sm-6 col-sm-push-6" style="margin-top: -560px">
<div class="row">
<h1 class="copy-title text-center"><%= @workout.name %></h1>
<h3 class="copy-subtitle">
Workout Type: </strong><%= @workout.workout_type %><br><br>
Goal: </strong><%= @workout.teaser %><br><br>
Workout Difficulty Level: </strong><%= @workout.difficulty %>
</h3>
<div class="text-center">
<%= link_to "Back to Your Workouts", workouts_path %><br>
<%# if current_user.admin? %>
<%= link_to 'Edit Workout', edit_workout_path(@workout) %> |
<%= link_to 'Delete Workout', workout_path(@workout), method: :delete %><br>
<%= link_to 'Add/Edit Exercises', workout_exercises_path(@workout, @exercise) %>
<%# end %>
</div> <!-- text-center -->
</div> <!-- row -->
</div> <!-- container overlap-hero-image -->
<% if flash[:notice] %>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash[:notice] %>
</div>
<div class="row" style="height: 40px"></div>
<% elsif flash.now[:alert] %>
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash.now[:alert] %>
</div>
<div class="row" style="height: 40px"></div>
<% elsif flash[:alert] %>
<div class="alert alert-warning">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash[:alert] %>
</div>
<div class="row" style="height: 40px"></div>
<% end %>
<div class="container col-sm-10 col-sm-push-1">
<div class="opaque-card md-well well">
<h1>The Workout</h1>
<p style="margin-bottom: 25px"><strong>Description: </strong><%= @workout.description %></p>
<iframe width="100%" height="450" src="https://www.youtube.com/embed/<%= @workout.video %>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
<h2>Report Your Scores</h2>
<% if @workout.exercises.count == 0 %>
<p>Looks like you get a freebie for this one! No score report today. Rest up and drink some water. It ain't always that easy...</p>
<% else %>
<% @workout.exercises.each do |exercise| %>
<%= render 'reports/form', report: @report, exercise: exercise %>
<% end %>
<% end %>
<h2>Your Previous Results</h2>
<% @workout.exercises.each do |exercise| %>
<h5><%= exercise.name %></h5>
<%= render exercise.reports, report: @report, exercise: exercise %>
<% end %>
</div> <!-- container -->
</div> <!-- md well -->
以下是它引用的reports/form
:
<%# if @exercise.reports.last != nil && @exercise.report.last.created_at < ( DateTime.now - (3/24.0)) %>
<%= form_for report,
:url => { :controller => "reports",
:action => :create,
:workout_id => @workout.id,
:exercise_id => exercise.id } do |f| %>
<div class="row">
<h5><strong><%= exercise.name %></strong></h5>
<% if exercise.needs_reps? %>
<div class="form-group col-xs-3">
<%= f.label :reps %>
<%= f.number_field :reps, class: 'form-control' %>
</div>
<% end %>
<% if exercise.needs_seconds? %>
<div class="form-group col-xs-3">
<%= f.label :seconds%>
<%= f.number_field :seconds, class: 'form-control' %>
</div>
<% end %>
<% if exercise.needs_weight? %>
<div class="form-group col-xs-3">
<%= f.label :weight %>
<%= f.number_field :weight, class: 'form-control' %>
</div>
<% end %>
<div class="col-xs-3"><%= f.submit "Record Results", class: 'btn btn-primary' %></div>
</div> <!-- row -->
<% end %>
<%# end %>
我的路线如下:
resources :workouts do
resources :exercises
end
resources :exercises, only: [] do
resources :reports
end
同样,一切都在localhost
完美无缺。我部署到Heroku时只收到错误消息。它不拒绝推送(即它表示部署成功),但当我尝试访问页面时,我收到We're sorry, but something went wrong.
错误。有没有人有这个问题的经验?如果可能的话我想避免完全重置我的数据库,因为我已经将信息放在其他表中,但如果有必要,我可以删除并重新加载workouts
数据表。
答案 0 :(得分:0)
看起来workout_path
返回nil
并且Rails路径帮助器Workout
会抛出错误,因为它需要id或model参数但得到nil
。
您的生产数据库(Heroku)中可能没有任何Workout
条记录,导致@most_recent_workout返回$ heroku login
$ heroku run rails console
pry(main)> Workout.create(...)
。
最快的修复方法,打开Heroku rails控制台并手动创建embedded
。
Gradle
一个好的做法是将种子包含在您的代码所依赖的任何部署中。