如何使用从模型到导轨中控制器的呼叫方法
我是Rails的新手,目前正在从事我的个人项目的功能。我有这些模型 - 位置,收藏夹,Thingstodo,用户。
用户,thingstodo和收藏夹具有多种关系,其中收藏夹是一个联接表。 /ploce(placeController)到目前为止,所有位置。我正在尝试渲染在其所有内容中拥有最喜欢的城市_to_do的城市,然后首先显示下一个在用户帐户中未添加为收藏夹的位置。
注意(ploce和things_to_do具有has_many关联)。
喜欢的模型-Group_by_favorite(方法名称)
class Favorite < ApplicationRecord
belongs_to :user
belongs_to :things_to_do
end
#validates :things_to_do_id, uniqueness: { scope: :user_id, :message => "has already been added as favorite" }
def group_by_favorite
Favorite.joins(:things_to_do,:user).group(:place_id).count
end
放置模型
has_many :things_to_dos
end
def self.top_places
@places_render = group_by_favorite.sort
end
位置控制器
class PlacesController < ApplicationController
def index
@places_render = top_places()
places = Place.all
render json: places
end
当我尝试运行 /位置路线时,我会收到此错误
"#<NoMethodError: undefined method `top_places' for #<PlacesController:0x00000000011aa8>>",
我的React代码 /位置
export default function DestinationContainer({user}){
const[allDestination,setAllDestination]= useState([])
const [search, setSearch] = useState("");
useEffect(()=> {
fetch("/places")
.then((res) => res.json())
.then((data) => {
setAllDestination(data)
})
},[])
const filterPlaces = allDestination.filter(
(destinations) =>
destinations.city.toLowerCase().includes(search.toLowerCase()) )
return (
<>
<Switch>
<Route exact path= "/places">
<SearchPlace search={search} setSearch={setSearch}/>
<DestinationView allDestination= {filterPlaces} user = {user} />
</Route>
<Route path="/places/:destinations" >
<ThingsToDoRender user ={user} />
</Route>
</Switch>
</>
)
}
是否可以解决此错误?我也尝试使用范围但不确定是否适用于这种情况。
I am fairly new to rails and currently working on the functionality of my personal project where I have multiple models. I have these models - Places, Favorites, ThingsToDo, User.
User, ThingsToDo, and Favorites have a many-many relationship where Favorites is a join table. /places (PlacesController) renders all the places as of now. I am trying to render cities that have the most favorites across all their things_to_do should be shown first followed by the next places that are not added by as favorites in user's account.
Note(places and things_to_do have has_many associations).
Favorite Model - group_by_favorite(method name)
class Favorite < ApplicationRecord
belongs_to :user
belongs_to :things_to_do
end
#validates :things_to_do_id, uniqueness: { scope: :user_id, :message => "has already been added as favorite" }
def group_by_favorite
Favorite.joins(:things_to_do,:user).group(:place_id).count
end
Place Model
has_many :things_to_dos
end
def self.top_places
@places_render = group_by_favorite.sort
end
Place Controller
class PlacesController < ApplicationController
def index
@places_render = top_places()
places = Place.all
render json: places
end
When I am trying to run /places route, I am getting this error
"#<NoMethodError: undefined method `top_places' for #<PlacesController:0x00000000011aa8>>",
My React Code for /places
export default function DestinationContainer({user}){
const[allDestination,setAllDestination]= useState([])
const [search, setSearch] = useState("");
useEffect(()=> {
fetch("/places")
.then((res) => res.json())
.then((data) => {
setAllDestination(data)
})
},[])
const filterPlaces = allDestination.filter(
(destinations) =>
destinations.city.toLowerCase().includes(search.toLowerCase()) )
return (
<>
<Switch>
<Route exact path= "/places">
<SearchPlace search={search} setSearch={setSearch}/>
<DestinationView allDestination= {filterPlaces} user = {user} />
</Route>
<Route path="/places/:destinations" >
<ThingsToDoRender user ={user} />
</Route>
</Switch>
</>
)
}
Is there a way that I can fix this error?I also tried to use scope but not sure if it is applicable in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您的错误所述,您的
PlacesController
类中未定义方法top_places()
。但它是一个Place
类函数。所以请尝试以下操作:As your error says, the method
top_places()
is not defined in yourPlacesController
class. But it is aPlace
class function. So try the following: