Rails: Cannot use methods defined in application helper within devise email views? - ruby-on-rails

I created some sort of mini cms, and I am trying to call a function
called render_content within the email confirmation view, but it just
won't take it :(
Alway complaining the method does not exist ?
I am not sure what I am doing wrong here...
Alex

Well after a while I figured it out, if the helper is located in the Application Helper, then this call will work:
ApplicationController.helpers.render_content()
Alex

Related

Rails - How to call a controller method from a different model?

I have a PagesController that handles all the static pages of my application. It has a method called join_session which renders the view pages/page_a.html.erb. This works.
My problem: I'm writing a method that renders the last viewed page when the user logs back in (using the Devise hook after_database_authentication in the User model).
Can I call the PagesController join_session method from the User model?
[I couldn't figure out how to do that, so I tried a hacky workaround: writing and calling a class method in PagesController, and rendering the view there, but since the name of the new method did not match the name of the controller method in the route, the variables in the view could not be resolved.]
My route:
get 'pages/page_a', to: 'pages#join_session'
Help would be much appreciated! :)
I think there is a pretty standard way to do this in your ApplicationController, you should check out this guide by Devise.
https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in
I hope this helps. If you have any questions let me know.

Ruby on Rails Expression in views

I'm new to rails and I got little confused when I seen this code in a view file.
<%= make_case_name payment, current_cart %>
What does the above code do.. Where to look for this payment variable?
Your above code aims to call the method make_case_name with two arguments: payment and current_cart.
As this code is embedded in your view, that means payment and current_cart are local variable. The method make_case_name is located in your helper.
Anyway, if it's belongs to a specific project that you're working on, you should contact with the project owner or developers for further details.
You have a following method which takes two parameters i.e payment and current_cart. Those are the local variables.
Structure is as follows
def make_case_name(payment, current_cart)
# your logic code
end
As per my assumption your make_case_name method may be present in the helpers.
Note: If you give few more details that would be helpful

How to use Monologue gem in native app (getting posts from Monologue into root app)

I'm using the Monologue gem (rails engine for blogging) and mounting it in my app at /blog, I want to get the posts with a certain tag and display those posts on the home page of my app ('/'). From rails c I can get a tag OR a post by doing Monologue::Tag or Monologue::Post but when I try to do Monologue::Tag.posts.published (like how he has the app getting the posts for a specific tag in source) I get "undefined method". I know it's because it's an engine, I'm just not familiar enough with engines to know what the proper syntax for this is?
Any help is much appreciated!
You are trying to get the posts for a specific tag right? Then, this is not what you want:
Monologue::Tag.posts.published
You are not specifying any tag here, you are calling the method posts on Monologue::Tag, but that is not a specific tag instance, it is the Monologue::Tag class.
Once you have a specific tag, you will be able to call posts.published on it, or as I see in the source, you can even call posts_with_tag
I guess that in order to try this, you could just get the first tag and then call the method:
Monologue::Tag.first.posts.published
Edit:
As I told you in the comment, your problem was that you were trying to call the method posts to an object that was not of the class Monologue::Tag (your model), but of the class ActiveRecord::Relation (a collection of Monologue::Tag objects).. Doing the first works, because is returning the first instance that was found by the where.
Another approach, if you know that you just want to fetch one instance, is to call find instead of where. Like this:
Monologue::Tag.find_by(name: "Goals")
This will return an instance of Monologue::Tag.

Ember.js: how to get my current_user info with no requests

I'm a beginner with ember.js, and i don't know the best way to do this:
my application has devise gem, and it's a server side mvc application until you log in. Then it's ingle page. So all my ember code is declared inside the "inner" layout. In this layout, obviously, i also have my current_user.name method. I would like to use this information, and not doing an http request to get it.
I think that if there could be a way to put it inside my applicationController... it would be perfect!
Any ideas? Or maybe i'm trying to do the wrong thing?
thanks,
Marco
You can embed your current_user's json in a data-current-user attribute on the body.
<body data-current-user="<%= current_user.to_json %>">
This way you can check if they are authenticated, and get their attributes without an HTTP call.
Ryan Bats goes into detail on this: http://railscasts.com/episodes/324-passing-data-to-javascript

Rails passing message to dynamic method

So I want to dynamically have new_course_discussion_path(#discussable) or new_group_discussion_path(#discussable) depending on the #discussable class type. How do I pass the message (#discussable) in to this method? I tried:
"new_#{#discussable.class.to_s.downcase}_discussion_path".constantize(#discussable)
but this says "wrong number of arguments (1 for 0)". I'm new to rails. Please help! Thanks.
Update:
Right now, the whole line is
= link_to '>New Discussion', "new_#{#discussable.class.to_s.downcase}_discussion_path".constantize(#discussable)
send "new_#{#discussable.class.to_s.downcase}_discussion_path", #discussable
The path helpers are methods not classes. They're accessible from controllers, views and helper methods and if you don't qualify the send call, it will invoke the method on self.
You are looking for the Object.send method.

Resources