I have some rails app. For example I have next models: User, Post.
User model was generated Devise. Post model has columns text and user_id. Relationship: User has_many :posts and Post belongs_to :user. I need to determine who is owner of a post. For example owner can edit his posts.
If I understand correctly I need to create some helpers. I am new at rails and don’t understand who do this. Can someone help me?
I have some rails app. For example I have next models: User , Post
Share
The term “helper” can be confusing. If you use the rails generators, you’ll get a file in the
app/helpersdirectory having the name<model>s_helper.rb. Here you can define methods that are directly accessible in your view templates. For example, you might define a method calledpronounbased on the value stored in thegenderfield of your User model, likeso in the view referencing some user, you could do
Of course you can do exactly the same thing in the model, and often that’s the better place to write helpers. For example, logic defining a user status might be
Then, wherever you can reference an instance of the user model, you can call
@user.status_nameGenerally, model methods are less about presentation and more about logic.