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
Related
I have a method on a model called Photo. I have it finding a selection of things from elsewhere in my app. All I need it to do at the end is to create a string of links that I can then output later on when the method is called on an instance.
My code is:
cars.map { |c| link_to(c.name, c) }.join(" AND ")
But i'm hitting this error:
undefined method `link_to' for #<Photo
Any ideas how to fix this?
link_to is a view helper which means it's only available in Rails views by default because it's a router / request concern.
If you specifically want to use link_to you have to include it or reference it directly.
See this SO answer
include ActionView::Helpers::UrlHelper
...
cars.map { |c| link_to(c.name, c) }.join(" AND ")
There are other ways of getting paths than using link_to that I would recommend you consider:
It's arguable that the Rails team would tell you to use UrlFor as the tip in that link suggests:
Tip: If you need to generate URLs from your models or some other place, then ActionController::UrlFor is what you're looking for. Read on for an introduction. In general, this module should not be included on its own, as it is usually included by url_helpers (as in Rails.application.routes.url_helpers).
UrlFor also allows one to access methods that have been auto-generated from named routes.
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
def base_uri
# named_route method that uses UrlFor
user_path(self)
end
end
User.find(1).base_uri # => "/users/1"
create your own concern to bring in route helpers via ActionMailer as this article suggests
As you may see if you scroll through other SO questions about including view helpers in models, there is pushback on using router and request -based methods outside of controllers and views because it violates the principles of MVC.
I think your use case can give you some peace of mind about this, but it's worth knowing the water is murky and some people may advise you otherwise.
The traditional Rails wisdom (and what I'm about to give you here) is that models should not be creating HTML. They also shouldn't have methods that return HTML. Creating HTML <a> tags should be done much closer to the user interface: in a view template or maybe in a view helper. One reason is that the particular way the hyperlink should be generated is a concern of the view. (Does it need a nofollow attribute? class attributes? This will change, even from one view to another.) And the model should not have any knowledge of these details.
When you do generate links in the views, then you have access to all the helpers such as link_to.
Instead, as I understand it, a model should be responsible for returning its own data. Maybe in your case that'd be an array of dicts of :label, :url. I.e., pure data that'd be easy to pass to link_to.
Hope that helps!
I'm new Rails guy and I have problem with the structure of Rails.
My application structure like this
- app
- controllers
-- orders_controller.rb
- services
-- get_customer.rb
OrdersController receive create order request and return new order. Because Order belongs to Customer. So I create a service name GetCustomer in order to find or create new customer if not exists.
However, the action method in GetCustomer service receive a long parameters. So I need to extract this parameter into another Object. But I don't know where place should I put it in ?
Any advice is graceful for me ! Thanks
Hi Your questions is not specific though here is my answer based on what you asked. In your service you can simply set an instance variable to the specific param that you want. example below:
def initialize(params)
#phone= params[:phone]
#something = params[:somthing]
#email = params[:email]
end
You would have to list specific code if you would like a more specific answer.
Hi I'm a beginner of rails and I'm not good at English. so if there is some total nonsense please understand..
I'm trying to record loading speed and page duration in every pages.
I made a database "pages" and method "savepage" in my "Page" model.
To save in every page I put "savepage" method in application controller.
Page.rb
def self.savepage
.
.
.
end
application_controller.rb
before_filter :dosave
def dosave
Page.savepage
end
these kind of format..
My question is
1. am I doing correct? using before_filter to do save in very first of loading process?
2. to save after loading all the contents in a page what should I use?
3. to save after user leave this page what should I use?
I saw before_destroy and after_filter, but I can't find what it is... what filter means.... what action means destroy....
thank you in advance!
before_filter is the first thing which loads before giving request to controller.But your need is completely different . Fundamentally filter are used boolean checking.If certain method is true,it will run otherwise it may not. This filter are further extended and we put code into that filters.(And Even sometimes it is consider as best practice) .
Now, before_filter :dosave might be right but is it not true way of knowing page(UI) loading process. I suggest you to use javascript call or use some manually created helper methods and place it into view .erb files.
May be this will interest you
https://github.com/grosser/record_activities
Log user activities in ROR
what action means ?
Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.
Source : http://guides.rubyonrails.org/action_controller_overview.html
I highly suggest you to read above documentation. It is very necessary for you and it covers topic which you asked here.`
And one more thing,
what is action destroy ?
This is simply an action method just like new. Since, rails follow Convention over configuration ( and its developer too) so they put code which do some delete destroy or some destruction. This make thing simple,otherwise more configuration will require which is against rails policy.
I am a rails newbie writing my first App and I am having a hard time figuring out exactly how views work. I am just trying to write a simple navigation menu that dynamically grabs the list of all my "Services" and then lists the sub-catagories "Service Offerings" under those. This navigation needs to show up on every page in my App.
I tried the following just to test if I could pull the data.
#application_helper.rb
def service_list
ServiceOffering.find(1)
end
#application.html.erb
<%= service_list.name %>
<%= yield %>
This works but I read that I should not put that sort of thing in the application_helper. I moved it to the application_controller.rb but it did not work there. I am really confused about this whole thing. Obviously, I have instance variables for #service_offering in my CRUD but they all mean different things depending on the action. I just want to define one that I can access from anywhere. I have watched and read a lot of tutorials in the past few weeks but hardly any of them touch on application wide stuff like this. I would really appreciate an easy to understand explanation of how to handle things of this nature. Thank you!
If you want to set it up in the application controller you have a couple options:
you could set it up with a helper method so
#application_controller.rb
helper_method :service_list
def service_list
ServiceOffering.find(1)
end
or you could set it up with a hook and put it into a special instance variable that will not conflict with the other ones that you have
#application_controller.rb
before_filter :service_list
def service_list
#special_service_offerice = ServiceOffering.find(1)
end
I've currently got this in my view file:
<%= "<em>(#{package.to_company})</em>" unless package.to_company.blank? %>
Is my understanding correct that I should move that to a helper?
ie.
def package_company(package)
"<em>(#{package.to_company})</em>" unless package.to_company.blank?
end
I ask because I've got a few dozen unless statements in this specific view based on if a user submits specific data or not. Seemed overkill to create a few dozen helper methods for just single unless statements.
Create this helper if you'are going to re-use this exact chunk of code many times (and stay DRY)… if you're going to use it once, you don't need an helper…