2014-11-13 1 views
0

기본적으로 저는 많은 활동이있는 테이블을 가지고 있으며, 각 활동에는 스포츠가 있으며, 활동의 이름을 연결하고 싶습니다 (스포츠 이름의 이름이기도합니다). 활동은 기자의 스포츠 페이지)에 연결되어, 나는 다음과 같은 오류가 점점 오전 : 나는 컨트롤러에서이 도우미 메서드를쿼리에서`model_name '이 정의되지 않았습니다.

<% @activity.each do |activity| %> 
<tr> 
<td><%= link_to activity.id, activity_path(activity) %></td> 
<td><%= link_to activity.name, searchsport(activity.name) %></td> //error here// 
<td><%= activity.ambiente %></td> 
<td><%= activity.resume %></td> 
<td><%= activity.duration %></td> 

:

undefined method `model_name' for Sport::ActiveRecord_Relation:Class 

여기보기는 내가 오류를 얻을 :

스포츠 테이블 :

create_table "sports", force: true do |t| 
    t.string "name" 
    t.string "description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

답변

1

다음과 같은 방법

link_to "Profile", profile_path(@profile) 
link_to "Profile", @profile 
link_to "Profile", controller: "profiles", action: "show", id: @profile 

link_to을 사용할 수 있습니다 그래서 그것은 url, or an active record object or a hash 소요 중 하나. 당신이주는 것은 ActiveRecord::Relation 개체입니다. 그래서 당신은 문제가 결과의 배열이 하나 개의 레코드를 얻기 위해 수정 반환 방법 searchsport입니다

helper_method :searchsport 
    def searchsport(name) 
    Sport.where(:name => name).first 
    end 
+0

감사합니다. 효과가있다. – MMrj

1

아래처럼 객체를 반환하는 도우미 방법을 변경해야합니다.

def searchsport(name) 
Sport.find_by_name(name) 
end 
관련 문제