I spent a few hours yesterday trying to figure this out and it is still frustrating me after taking a break, so I figured that I would ask. Also, sorry if this post is long and my use of terminology is amateurish. I am still really new to programming.
我昨天花了几个小时试图弄明白这一点,但在休息之后仍然让我很沮丧,所以我想我应该问一下。同样,抱歉,如果这篇文章很长,我使用的术语是业余的。我对编程还是很陌生的。
I am trying to make a rails app using the 'themoviedb' gem that will allow users to search themoviedatabase's api. Here is an example project provided by the creator of the gem, which uses an older version of rails: https://github.com/ahmetabdi/themoviedb-example. I am trying to recreate this sample app before I start making my own changes, but I am running into trouble with the MoviesController.
我正在尝试使用“themoviedb”gem创建一个rails应用程序,它允许用户搜索themoviedatabase的api。这里是gem的创建者提供的一个示例项目,它使用了一个更老的rails版本:https://github.com/ahmetabdi/themoviedb-example。我正在尝试重新创建这个示例应用程序,然后开始自己做修改,但是我遇到了MoviesController的麻烦。
Here is the example MoviesController:
以下是MoviesController的示例:
class MoviesController < ApplicationController
def show
@movie = Tmdb::Movie.detail(params[:id])
@images = Tmdb::Movie.images(params[:id])
@cast = Tmdb::Movie.casts(params[:id])
@trailers = Tmdb::Movie.trailers(params[:id])
@similar_movies = Tmdb::Movie.similar_movies(params[:id])
end
end
And here is part of the example Movies#Show View:
下面是一些例子电影的例子:
<h3>Poster</h3>
<%= image_tag("#{@configuration.base_url}w154#{@movie.poster_path}") if @movie.poster_path %>
<h3>Facts</h3>
Budget: $<%= number_with_delimiter(@movie.budget, :delimiter => ',') %><br />
Revenue: $<%= number_with_delimiter(@movie.revenue, :delimiter => ',') %><br />
Status: <%= @movie.status %><br />
Runtime: <%= @movie.runtime %><br />
If I try to run my application with this example code, then I am given this error:
如果我尝试用这个示例代码运行我的应用程序,那么我就会得到这个错误:
NoMethodError in Movies#show
undefined method `poster_path' for #<Hash:0x007fb615913de0>
app/views/movies/show.html.erb:5:in `_app_views_movies_show_html_erb___4006517199107153671_70210007215400'
My solution was to add a helper method to the MoviesController and refactor the code of the Movies#Show View to fix the hash element error:
我的解决方案是向MoviesController添加一个helper方法,并重构电影#Show视图的代码来修复哈希元素错误:
class MoviesController < ApplicationController
helper_method :movie
def show
@movie = Tmdb::Movie.detail(params[:id])
@images = Tmdb::Movie.images(params[:id])
@cast = Tmdb::Movie.casts(params[:id])
@trailers = Tmdb::Movie.trailers(params[:id])
@similar_movies = Tmdb::Movie.similar_movies(params[:id])
end
private
def movie
@movie = Tmdb::Movie.detail(params[:id])
images = Tmdb::Movie.images(params[:id])
end
end
#views/movies/show.html.erb
<h3>Poster</h3>
<%= image_tag("#{@configuration.base_url}w154#{movie[:poster_path]}") if movie[:poster_path] %>
<h3>Test data</h3>
Runtime: <%= movie[:runtime] %><br />
By making these changes, I can get the page to load with the correct ID, but none of the rails commands seem to run, so I just end up with a page that shows the html, but none of the pulled information is displayed.
通过做这些更改,我可以让页面使用正确的ID加载,但是没有一个rails命令看起来运行,所以我只使用一个显示html的页面,但是没有显示被拉的信息。
Here is what shows in the terminal when I try to pull up a movie:
这是我试图拉起一部电影的时候在终端放映的东西:
Started GET "/movies/329" for 127.0.0.1 at 2015-04-19 14:32:07 -0700
Processing by MoviesController#show as HTML
Parameters: {"id"=>"329"}
Rendered movies/show.html.erb within layouts/application (900.5ms)
Rendered layouts/_header.html.erb (0.8ms)
Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 2083ms (Views: 1011.1ms | ActiveRecord: 0.0ms)
I think that I am just having some sort of syntax issue, but I honestly don't know. I think I also might be overthinking things. My SearchController and views are working properly, I just can't get the results with this controller/view though.
我想我只是有一些语法问题,但我真的不知道。我想我也可能会过度思考。我的SearchController和视图正在正常工作,但是我无法通过这个控制器/视图得到结果。
I really appreciate any help or advice to help me figure this out.
我非常感激能帮我解决这个问题的任何帮助或建议。
1 个解决方案
#1
0
In the first case, when you had in the view the following line:
在第一个例子中,当你在视图中有如下一行:
@movie.poster_path
you got the NoMethodError
because @movie
is a Hash returned by Tmdb::Movie.detail(params[:id])
, and a Hash
object doesn't have a poster_path
method. The right way to access this is just @movie[:poster_path]
, as you have almost written in your second attempt.
您获得了NoMethodError,因为@movie是Tmdb所返回的散列::Movie.detail(params[:id]),而散列对象没有poster_path方法。正确的访问方式是@movie[:poster_path],正如您在第二次尝试中所写的那样。
However, your second try doesn't work because movie
is not the same as @movie
. When you use the helper method you created in the view, movie
, you aren't accessing the @movie
Hash. As it's defined, it returns the result of the last line executed, that in your case is
但是,你的第二次尝试失败了,因为电影和@movie不一样。当您使用在视图中创建的helper方法时,您不会访问@movie散列。当它被定义时,它返回最后一行执行的结果,在您的情况中是这样。
images = Tmdb::Movie.images(params[:id])
So that's the value for movie
, and since it doesn't have a :poster_path
key, nothing is shown.
这就是电影的价值,因为它没有:poster_path键,没有显示。
I believe you can make this works by removing your helper method and using @movie["poster_path"]
in your view. I haven't used the themoviedb
gem before, though, but I think that would be the way to go.
我相信您可以通过删除您的助手方法和使用@movie[“poster_path”]来实现这一功能。虽然我之前没有使用过themoviedb gem,但是我认为这是一种方法。
Updated
更新
It looks like the hash returned by Tmdb::Movie.detail(id)
has strings as keys rather than symbols, and it's not a HashWithIndifferentAccess
. Elements should be accessed using @movie["poster_path"]
rather than @movie[:poster_path]
.
它看起来就像Tmdb所返回的散列::Movie.detail(id)将字符串作为键,而不是符号,并且它不是一个hashwithin差分访问。元素应该使用@movie["poster_path"]来访问,而不是@movie[:poster_path]。
#1
0
In the first case, when you had in the view the following line:
在第一个例子中,当你在视图中有如下一行:
@movie.poster_path
you got the NoMethodError
because @movie
is a Hash returned by Tmdb::Movie.detail(params[:id])
, and a Hash
object doesn't have a poster_path
method. The right way to access this is just @movie[:poster_path]
, as you have almost written in your second attempt.
您获得了NoMethodError,因为@movie是Tmdb所返回的散列::Movie.detail(params[:id]),而散列对象没有poster_path方法。正确的访问方式是@movie[:poster_path],正如您在第二次尝试中所写的那样。
However, your second try doesn't work because movie
is not the same as @movie
. When you use the helper method you created in the view, movie
, you aren't accessing the @movie
Hash. As it's defined, it returns the result of the last line executed, that in your case is
但是,你的第二次尝试失败了,因为电影和@movie不一样。当您使用在视图中创建的helper方法时,您不会访问@movie散列。当它被定义时,它返回最后一行执行的结果,在您的情况中是这样。
images = Tmdb::Movie.images(params[:id])
So that's the value for movie
, and since it doesn't have a :poster_path
key, nothing is shown.
这就是电影的价值,因为它没有:poster_path键,没有显示。
I believe you can make this works by removing your helper method and using @movie["poster_path"]
in your view. I haven't used the themoviedb
gem before, though, but I think that would be the way to go.
我相信您可以通过删除您的助手方法和使用@movie[“poster_path”]来实现这一功能。虽然我之前没有使用过themoviedb gem,但是我认为这是一种方法。
Updated
更新
It looks like the hash returned by Tmdb::Movie.detail(id)
has strings as keys rather than symbols, and it's not a HashWithIndifferentAccess
. Elements should be accessed using @movie["poster_path"]
rather than @movie[:poster_path]
.
它看起来就像Tmdb所返回的散列::Movie.detail(id)将字符串作为键,而不是符号,并且它不是一个hashwithin差分访问。元素应该使用@movie["poster_path"]来访问,而不是@movie[:poster_path]。