Я новый разработчик rails, у которого есть базовое ядровое приложение, которое я немного модифицировал.Отсутствие ошибки ошибки ошибки
Я получаю эту ошибку:
undefined method description for #<ActiveRecord::Relation:0x00000102df26d8>
, когда я посещаю john/recipes/46
. Вот мое мнение:
<h1 itemprop="name"><%= @recipe.name %></h1>
<ul>
<li><%= link_to 'Edit', edit_recipe_path(@recipe) %></li>
</ul>
<p itemprop="description"><%= @recipe.description %></p>
вот мои маршруты:
match "/:username" => "recipes#index"
scope ':username' do
resources :recipes
end
здесь мое шоу индекс:
def show
@user = User.find_by_username params[:username]
@recipe = Recipe.where(:user_recipe_id => params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @recipe }
end
end
и моя модель:
before_save :set_next_user_recipe_id
belongs_to :users
validates :user_recipe_id, :uniqueness => {:scope => :user_id}
def to_param
self.user_recipe_id.to_s
end
def set_next_user_recipe_id
self.user_recipe_id ||= get_new_user_recipe_id
end
def get_new_user_recipe_id
user = self.user
max = user.recipes.maximum('user_recipe_id') || 0
max + 1
end
attr_accessible :description, :duration, :author, :url, :name, :yield, :ingredients_attributes, :user_recipe_id, :directions_attributes, :tag_list, :image
Причина Я делая Recipe.where(:user_recipe_id => params[:id])
вместо Recipe.where(:id => params[:id])
- это потому, что я пытаюсь получить это вместо john/recipes/46
, показывая 46-й рецепт в базе данных, вместо этого покажу 46-й рецепт, принадлежащий Джону.
Спасибо за помощь!
Спасибо за ответ. К сожалению, когда я это делаю, я получаю 'undefined имя метода для nil: NilClass?' Что это могло быть? –
Это означало, что не было рецепта с предоставленным 'user_recipe_id'. –
, пожалуйста, измените свою таблицу сопоставлений own_to: user. И когда вы хотите показать рецепт имени, используя @ recipe.user.name. – akbarbin