在尝试访问我的某个模型workouts#show
的节目视图时,我收到的错误是:
undefined method `stringify_keys' for "/workouts/abs-0002":String
它在_template.html.erb
中的一个链接上调用它,该链接在我的workouts#show
页面中呈现(第一行调用错误):
<%= link_to "Do this one!", workout_path(workout) do %>
<p class="cta">Pick me!</p>
<% end %>
我的workouts
控制器是:
class WorkoutsController < ApplicationController
def index
@workouts = Workout.all
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_id = params[:workout][:user_id]
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, :user_id)
end
end
我的workout.rb
模型是:
class Workout < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
belongs_to :user
has_many :exercises
has_many :reports
validates :user, presence: true
end
任何人都可以帮我看看这里出了什么问题吗?
答案 0 :(得分:2)
根据http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to,如果要为链接使用自定义名称,则需要删除第一个参数:
如果您的链接目标很难适应name参数,也可以使用块。 ERB例子:
<%= link_to(@profile) do %>
<strong><%= @profile.name %></strong> -- <span>Check it out!</span>
<% end %>
# =>
<a href="/profiles/1">
<strong>David</strong> -- <span>Check it out!</span>
</a>