Ruby on Rails: Can't implement Star Rating system

血红的双手。 提交于 2019-12-04 19:57:46

From the comments, the error seems to be here:

@hotel.ratings.find_or_create_by_user_id user.id

--

user_id

The problem is your show view doesn't have access to a local variable called user

This variable should either be defined in the controller (which would mean it has to be an @instance variable, or should be a helper (such as current_user.id)

The fix should therefore be as follows:

<% user_id = user_signed_in? ? current_user.id : "-1" %>

<%= form_for @hotel.ratings.find_or_create_by_user_id user_id ...

This should get it working for you with the code you have provided. As you've not provided the new action from the controller, I don't know whether the supporting structure for the code will be correct or not.

After some search on find_or_create_by, I changed line with 'form_for' into

<%= form_for @hotel.ratings.find_or_create_by(user_id: user_id)

That solved the issue!

Thanks to all for your support!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!