Rails form helpers with devise - ruby-on-rails-3

I am using rails 3.2.3 with devise to sign up and sign in users.
The problem is that devise (I might be wrong here) somehow overrider the interpretation of form helpers where an error in the form occures (for example, the registrations/new form). It puts the labels and the inputs, wich are wrong in some kind of div blocks and ruines the view.
How can I customize that behavior?
UPDATE 1
The views/devise/registrations/new.html.erb looks like this: https://gist.github.com/2585197
And here is the output if errors occur: https://gist.github.com/2585208

You will need to generate views for the model. For example
rails generate devise:views users
This gives you control over the views you get from devise. Check out the documentation here
https://github.com/plataformatec/devise

Related

Devise: Load a partial instead of showing a notice for non confirmed members

Is there a way to load a view for no confirmed users that login?
Default behaviour is to show a notice: " You have to confirm your account before continuing."
I tried
overrule the sessions#create method of devise checking for current_user.confirmed_at.blank?
in the after_singin_path check for current_user.confirmed_at.blank? and render the view instead
My goal is to render a custom view instead of the notice but cannot hook into the right location. Who knows how to accomplish this? thx!
You can simply copy the code from the devise github and place in your controllers/devise. then change any action or method you want to.
You may also just extend the devise session controller and override any action you want to.
class Abc < Devise::SessionsController
# this just reopens the class.
# Remember classes are never "closed" in ruby!
end
I like the ruby way of solving this, I guess that in your UsersController after a POST request the user will be returned and signed in using the sign_in(Object) helper Devise provides.
Also I suggest using a confirmed boolean instead of timestamp.
Why not check for the value using an if else statement the ruby way:
user.confirmed ? sign_in(user) : render :partial => 'path/partial'
Hope this might help you out

ruby on rails 3, render multiple views in one view

I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan
If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails

Customizing devise layouts for registrations vs. passwords

I have an application that uses devise for authentication. I am trying to customize the layouts for the devise controller, and I was able to do some of this by following the answers to another question here on Stack Overflow. However, I can't find anything about how to distinguish between the devise/passwords and devise/registrations controller. I am using the following code:
def layout_by_resource
if devise_controller?
if action_name == "edit" or action_name == "update"
"application"
else
"sessions"
end
else
"application"
end
end
The problem is that when a user tries to re-set their password, it's trying to use the application layout (since the action is edit). I need it to use the sessions layout. Can anyone help me figure out how to make that happen?
You should look at this response on different layout for sign_in action in devise, which highlights the Devise docs on How To Create Custom Layouts

Setting Formtastic as Rails 3 default form builder, is it possible?

Something like this in application.rb:
# Configure application generators
config.app_generators do |g|
g.form_builder Formtastic::SemanticFormBuilder
end
If I do so I get an error when I try to scaffold a model:
Expected Thor class, got Formtastic::SemanticFormBuilder
Is it possible to set Formtastic as default form builder?
Updated.
I have tried Simple forms and it's really awesome (Thanks to nathanvda). The DSL is almost the same as Formtastic has. The only important difference for me is in customizing button labels. In formtastic it's possible to use resource file (formtastic.yml) to set different labels for the same model and action. Sometimes it's necessary, for example in Devise views. But it costs nothing to switch from formtastic to simple forms even in this case as it's possible to do it in this pretty simple way:
= f.submit t("customized_button_label")
Now about the original question. When I installed simple forms it creates template in lib/templates/haml/scaffold directory which will be used with scaffold. Straightforward.
I am not entirely sure about formtastic, either it does this straight out of the box, so not configuration needed; or not at all.
But what i do know: simple_form does provide scaffolding, even configurable which is totally awesome. The DSL between formtastic and simple_form is close to identical, but with simple_form the level of configuration is much better. You have total control how a form should be scaffolded, you have total control how a single field is turned into html. Pretty awesome.
You can find a quick introduction here.

Rails : Can I use forms from the public/index.html file?

Am currently using Rails 3.0 to develop my app.
How to handle a form in the public/index.html file which I'm planning to use as my Home page.
Usually a view's action buttons gets paired with the corresponding method of its controller.
But how to handle this in case the index file in public directory?
This is possible, so long as the form's action attribute is pointing at an action of a Rails controller. However, it is not the norm in Rails to use a static HTML page to capture data. The norm is to use the MVC architecture of Rails.
I have to ask: Have you read the documentation provided on the Ruby on Rails website? Check out http://edgeguides.rubyonrails.org and read the Getting Started section.
Now, I think you might be using public/index.html because you don't know how to use a controller to serve the root of your website. Here's a quick example of how to use a controller instead of public/index.html:
Create a controller (i.e., home via rails g controller Home index)
Modify routes.rb and add root :to=>"home#index"
Delete public/index.html
The above is actually in the Edge Guides, Getting Stated section. So again, I strongly recommend you read the documentation. It will honestly save you a lot of trouble.