Reputation: 1923
I have a simple FriendsController
class FriendsController < ApplicationController
helper_method :get_friends_array
def create
@bill = Bill.find(params[:bill_id])
@friend = @bill.friends.build(friend_params)
if @friend.save
add_friends_array(@friend.name)
redirect_to bill_path(@bill)
else
redirect_to root_path
end
end
def destroy
@bill = Bill.find(params[:bill_id])
@friend = @bill.friends.find(params[:id])
@friend.destroy
redirect_to bill_path(@bill)
end
# None CRUD methods
friends_array = Array.new
def add_friends_array(name)
friends_array << name
end
def get_friends_array
friends_array
end
private
def friend_params
params.require(:friend).permit(:name)
end
end
and when I try to call the following line in one of my form in the view <p><%= f.label :payer %> <%=f.select :payer, options_for_select(get_friends_array)%></p>
it returns a undefined local variable or method
get_friends_array'`
Why is that? Is it because I am calling a method within another method? Or is it something else entirely?
thanks!
EDIT: Does it have to do with the fact that the view is not a Friends View? I'm actually calling get_friends_array
in another Model's view. If that's the case, what's the best way to solve this problem?
Upvotes: 0
Views: 107
Reputation: 1896
Why is that? Is it because I am calling a method within another method?
No
Does it have to do with the fact that the view is not a Friends View? I'm actually calling get_friends_array in another Model's view. If that's the case, what's the best way to solve this problem?
Mostly likely yes, the page you are trying to use this helper method might be coming from another controller and not FriendsController
.
If friend names are coming from your database you should be able to populate array by doing a pluck
: Friend.pluck(:name, :id)
More info about helper methods - https://stackoverflow.com/a/3993323/753705
Upvotes: 1