I have a Rails 4 project using Ruby 2.0. I've defined some refinements. Putting
<% using MyRefinements %>
at the top of the view files causes the error "undefined method 'using'".
When I add:
using MyRefinements
At the top of my controller (above the class declaration), I can successfully use the refinement in the controller, but I get an 'undefined method' error if I try to use it in the view.
Thanks!
Unfortunately, this does not seem possible. For posterity, I'm documenting the things I tried that didn't work:
In the view:
<% using MyRefinements %>
In the controller (each tried separately):
using MyRefinements
helper :MyRefinements
helper_method :MyRefinements
helper { using MyRefinements }
In the helper:
using MyRefinements
Note that refinements become available in the controller and in the helper, but never in the view.
Too bad.
By using 'using' you can import class refinements from module into the current class or module definition.
You can not include it in view file by 'using'.
if you want to use it in view you can do following in your controller(I have not tested it):
using MyRefinements
helper :MyRefinements OR helper_method :MyRefinements
Related
I have a helper defined in my application_helper.rb that I would like to use in my mailer view, but just doing
<%= helper_method(param) %>
in the mailer_view.html.erb file gets me an 'undefined method' error.
Is there another way to do this? I would hate to have to put the same helper somewhere else.
Thanks.
Apparently this has been asked before (who knew!) :). The answer is to include
helper ApplicationHelper
in the example_mailer.rb file:
class ExampleMailer < ApplicationMailer
helper ApplicationHelper
...
end
I am having a login page for user generated by devise gem.where I removed the registerable from model for only login not for signup.I have also generated an admin which gives default login credentials for user in seed.rb file.I have done some css work in login page. next I have generated an employee page for further process for users.
Here my doubt is the styling part what I have done in login page is also coming in employee page. I dont want that so I wrote some condition in application.html.erb
<% unless user_signed_in? %>
<%= render 'layouts/heading' %>
<% end %>
The method is defined in application.controller.rb
def user_signed_in? %>
render :layouts => 'application'
end
I tried a lot by changing conditions but it is still displaying or showing errors.
You said that you're using "device gem", but since you're talking about authentication, I assume you mean "devise"?
The user_signed_in? method is a helper which should be provided automatically by devise, and you should be able to use it in your views. You shouldn't need to define it in the application controller.
You probably want to make sure you do "before_filter :authenticate_user!" in the relevant controller (you can use the ":only" option if you only want this to apply for certain methods).
It's difficult to give any more information because you haven't said what errors you are getting. I assume you're getting some kind of syntax error on the definition of the "user_signed_in?" method, since you have an invalid "%>"on the end of the line, which I guess you copied-and-pasted from an ERB file.
Is there a better way of using say the blue spree theme on other non spree pages?
I'm just going through and including the files that I can and adding the HTML I can't get from the includes into a master template. Would be nice if there was a better / easier way.
After some digging I figured it out.
All you have to do is set your controller to inherit from the Spree::StoreController
so go into whatever controller you want to do this for and change this:
class FooController < ApplicationController
def index
end
end
To this:
class FooController < Spree::StoreController
def index
end
end
I'm a rails beginner learning rails 4 and I'm trying to learn by doing. I'm making a simple blog that I want some simple user authentication on. I'm trying to learn here, so I don't want to implement Devise, etc. I have a header partial that takes care of my site header and I'm trying to put a link to logout that only shows if a user is logged in. I have a simple session controller that has a new action for the signup form, a create action that sets the current user after matching the email and password and sets session[:user_id] = #current_user.id, and a destroy action that nils out the session. In my application controller I have a method like this
def logged_in?
!session[:user_id].nil?
end
In my _header.html.erb partial, I have
<% if logged_in? %>
(My link)
<% end %>
When I load the page it tells me it can't find the "logged_in?" method. Anyone know why? Thanks.
Methods on controllers are by default not exposed to the views (which your partial is part of).
2 solutions:
Create your logged_in? method as a helper method, for example in an AuthenticationHelper. Doing this you cannot access it from controllers, though.
Expose your controller method to the view using helper_method:
class ApplicationController < ActionController::Base
helper_method :logged_in?
def logged_in?
[...]
end
end
Couldn't you just use:
if #current_user
I'm using a pagination example from balbus design. In the .ss template, there is a line of code:
<% control ProductList.PaginationSummary(5) %>
Is that possible to use a variable instead of hard-coding the value 5? For example:
<% control ProductList.PaginationSummary(PSSize) %>
The variable PSSize is defined in the model and will return the number set in the CMS.
The SS 2.4 templating language is very limited in terms of what it can do.
In this specific case, you could try working it out in the controller - try adjusting the $resultSet within ProductListPage_Controller::ProductList to preprocess the pagination summary to desired context size, so you can access it later from the template.
Try something like that:
$resultSet->AdjustedPaginationSummary = $resultSet->PaginationSummary($this->productsPerPage);
return $resultSet;
And then in the template you should be able to do:
<% control ProductList.AdjustedPaginationSummary %>