Answer the question
In order to leave comments, you need to log in
Using @var in link_to
Gentlemen ruby, help
me I'm trying to revive the link in the _header format
<%= link_to @company.name, current_company %>
First Doper got
NoMethodError in StaticPages#about - undefined method `name' for nil:NilClass
that the Static_pages controller does not know about any Companies, and therefore it was decided to change the static pages controller to:
def home
@company = Company.new
end
def about
@company = Company.new
end
And so on. This, however, did not help much - the links instead of the name look like
/companies/2
How to solve the problem? Is it possible to do this without repeating in every controller method? Thank you!
Answer the question
In order to leave comments, you need to log in
If you need the variable to be available in all (well, or a large number) methods, then you can do this:
before_filter :init_company
...
private
def init_company
@company = Company.find(params[:id])
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question