如何使用从模型到导轨中控制器的呼叫方法

发布于 2025-01-19 05:02:10 字数 2190 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2025-01-26 05:02:10

正如您的错误所述,您的 PlacesController 类中未定义方法 top_places() 。但它是一个 Place 类函数。所以请尝试以下操作:

@places_render = Place.top_places()

As your error says, the method top_places() is not defined in your PlacesController class. But it is a Place class function. So try the following:

@places_render = Place.top_places()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文