Rails passing message to dynamic method - ruby-on-rails

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.

Related

How to find and use a helper method from String

I would like to know how to find and use a helper method, when I have the name of the helper in a string format.
I am writing an engine, and in the initializer file, users can specify the current_user_helper as such:
MyEngine.current_user_helper = "current_user"
I would like to do something with this in the controller. For instance, the code below tries to create a Post for the logged in user.
MyEngine.current_user_helper.posts.new(post_params)
Yet, this code does not work because MyEngine.current_user_helper is a string. Apparently, Rails will throw an error when I try:
MyEngine.current_user_helper = current_user
without the quotation marks.
Any suggestions?
You can use send to call a method from a string or symbol. send is available on all descendants of Object.
send(MyEngine.current_user_helper).posts.new

passing named_scope as argument

I am reusing a controller method and I need to change the scope as required.
I have stored the scope name in a session and would like to be able to do the following.
if params[:scope_name]
session[:submission_scope_name] = params[:scope_name]
else
session[:submission_scope_name] = "allSubs"
end
#search = Submission.session[:submission_scope_name].search do
...
end
The code above is giving me the following error message:
undefined method `session' for #<Class:0x00000002ad7df0>
Is there any way of passing a named_scope as an argument?
You probably don't want to do this from a security standpoint: a malicious user could make a poorly-formed submission_scope that you'll just be sending straight to Submission.
That said, you're looking for the method send here. Try this instead:
Submission.send(session[:submission_scope_name].to_sym).search
send will try to call a method on that object named whatever symbol you passed in. You can read more about it in the Ruby core docs, but ultimately doing that would allow you to send whatever named scopes to Submission you want.

Ruby on Rails - Call controller method from a view

Sorry, I have a Rails newbie question. In my Rails application, how can I call a method defined in my controller from the view? For example, let's say I wrote a custom method that retrieves stock information. In my view, I want to have a regular HTML button, that upon clicking, will call my custom method and populate the stock results.
How is that done? I've looked around and couldn't find a straightforward way to do this. But I'm sure I'm missing something here.
Edit: I took the question by its title which wasn't what the question was. The answer to the title of your question is at the bottom
What you want is an ajax request, which is a separate controller action. You'll need:
javascript to request a route and populate its DOM object when the button is clicked
an action that returns whatever the ajax request was asking for
There are many ways to do this, but if you search for "howto rails ajax" you'll find a gazillion tutorials to help you on your way. One way that I like is called pjax: https://github.com/rails/pjax_rails
Old answer...
Declare a helper_method, like so:
In your controlller:
private
def find_stock
...
end
helper_method :find_stock
In your view:
<% find_stock.each do |stock| -%>
Documentation: http://api.rubyonrails.org/classes/AbstractController/Helpers/ClassMethods.html#method-i-helper_method
You can check the documentation for the button_to view helper
This is one way, of course, you could use the remote option to do this via an AJAX request.

Rails: Cannot use methods defined in application helper within devise email views?

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

Why can't I use a param called "action"?

Is "action" as a input field name forbidden? Because everything works except the assignment of the "action" param.
because action, controller are prohibited words.
Look around debug params
--- !map:ActiveSupport::HashWithIndifferentAccess
action: index
controller: main
so you can't use those params. Because they will be REWRITED AUTOMATICALLY
I would suggest NOT using words like action, name, method as field names as they are all attributes of the form tag and are likely to get confused when the form is posted
I agree with jbeynon, I would also say anything that has to do with CRUD(Create, Read, Update, Delete) is protected also.
I don't see why this would be invalid. You'd want to avoid conflicting with existing class or method names (e.g. not a good idea to define a method called action on a controller).
everything works except the assignment
of the "action" param.
Does this generate an error? If so, what exactly?

Resources