*Show* 视图始终显示数据库中的第一个 pin(项目)
*Show* view always shows the first pin (item) in the database
我正在开发一个类似 pinterest 的应用程序,最近遇到了一个问题。
当我作为登录用户尝试通过 "show" 访问一个 pin 时,它会在 url 中为我提供正确的 ID 号,例如http://localhost:3000/pins/7 但描述来自数据库中的第一项。
这是我的显示视图代码:
<%= image_tag @pin.image.url %>
<p>
<strong> Description: </strong>
<%= @pin.description %>
</p>
<% if @pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(@pin) %>
<% end %>
<%= link_to 'Back', pins_path %>
另一方面,当我尝试通过 heroku 通过 show 或编辑访问它时,它以以下消息结束:
"很抱歉,出了点问题。如果您是应用程序所有者,请查看日志以获取更多信息。"
这是我的 github 回购 https://github.com/LeJaques/myfirstapp_new
我将不胜感激!
看起来您使用 find_by
设置 @pin
不正确。
def set_pin
@pin = Pin.find_by(params[:id])
end
尝试将其更改为 Pin.find(params[:id])
或添加一个字段以按 Pin.find_by(id: params[:id])
进行搜索
您的问题似乎出在 PinController
中的 set_pin
方法中。
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find_by(params[:id])
end
当简单地将 params[:id]
传递给 find_by
时,您将 运行 陷入困境。您应该使用
@pin = Pin.find(params[:id])
或
@pin = Pin.find_by(id: params[:id])
你在后来的 correct_user
方法中奇怪地使用了后者。
与您的问题无关,但在未来,与其链接到您的 github 项目并让我们深入研究代码,请 post 与您的问题相关的代码。这样做将有助于您在未来更快地获得答案;如果你的 set_pin
方法从一开始就在你的问题中,这个问题会在五分钟内得到回答(而不是半小时以上)。
我正在开发一个类似 pinterest 的应用程序,最近遇到了一个问题。 当我作为登录用户尝试通过 "show" 访问一个 pin 时,它会在 url 中为我提供正确的 ID 号,例如http://localhost:3000/pins/7 但描述来自数据库中的第一项。
这是我的显示视图代码:
<%= image_tag @pin.image.url %>
<p>
<strong> Description: </strong>
<%= @pin.description %>
</p>
<% if @pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(@pin) %>
<% end %>
<%= link_to 'Back', pins_path %>
另一方面,当我尝试通过 heroku 通过 show 或编辑访问它时,它以以下消息结束:
"很抱歉,出了点问题。如果您是应用程序所有者,请查看日志以获取更多信息。"
这是我的 github 回购 https://github.com/LeJaques/myfirstapp_new
我将不胜感激!
看起来您使用 find_by
设置 @pin
不正确。
def set_pin
@pin = Pin.find_by(params[:id])
end
尝试将其更改为 Pin.find(params[:id])
或添加一个字段以按 Pin.find_by(id: params[:id])
您的问题似乎出在 PinController
中的 set_pin
方法中。
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find_by(params[:id])
end
当简单地将 params[:id]
传递给 find_by
时,您将 运行 陷入困境。您应该使用
@pin = Pin.find(params[:id])
或
@pin = Pin.find_by(id: params[:id])
你在后来的 correct_user
方法中奇怪地使用了后者。
与您的问题无关,但在未来,与其链接到您的 github 项目并让我们深入研究代码,请 post 与您的问题相关的代码。这样做将有助于您在未来更快地获得答案;如果你的 set_pin
方法从一开始就在你的问题中,这个问题会在五分钟内得到回答(而不是半小时以上)。