简体   繁体   中英

Rails View haml “undefined method” link_to

Ruby, Rails 3, newbie

I am extending a working Rails application (displays movies) for a class assignment, so the basic plumbing works OK.

I added an entry to routes.rb -

match "/movies/directed_by/:director" => "movies#directed_by"

my rake routes then has an entry

/movies/directed_by/:director(.:format) {:controller=>"movies", :action=>"directed_by"}

notice there is no "path", nor HTTP method (eg GET) specified

For the other (Rails Model auto-generated) routes, paths and verbs are specified.

I have a controller method

def directed_by

  # some code

end

I have a View file ../app/Views/show.html.haml where I added

= link_to 'Find Movies With Same Director', directed_by(@movie.director)

which throws a runtime error -

NoMethodError in Movies#show

Showing /home/saasbook/hw3/hw3_rottenpotatoes_rjf/app/views/movies/show.html.haml where line #22 raised:

undefined method `directed_by' for #<#<Class:0xc564594>:0xc532198>

So my question is, now do I tweak my routes.rb and/or view haml to get everything tied together?

All hints welcomed.

In routes:

match "/movies/directed_by/:director" => "movies#directed_by", :as => :movie_direct_by

Then in your view:

= link_to 'Find Movies With Same Director', movie_direct_by_path(@movie.director)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM