Using Devise, I don't see notices like "you've signed in" when i log into my app - ruby-on-rails-3

I'm building a basic app with Rails 3.2 and Devise 2.0. I've create a User devise model and a Projects model. In my routes.rb files I have
root :to => 'projects#index'
I can sign up and sign in at Get users/sign_up and Get users/sign_in, respectively, but when it redirects to projects#index, I don't see a notice at the top that says "You've signed in successfully. Which file do I need to check to fix this?

You need to add flash messages showing to the file /projects/index.html.erb
for example this way:
<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>

Related

how to pass id in controller

I have this error . I'm using users using Omniauth-identity and Omniauth-FB TWITTER from railscast video.
No route matches [GET] "/users/generators/new"
when i click
<%= link_to 'GENERATE RAPD PRIMER',new_user_generator_path(#user), id:'new' %></li>
What this route does in rake routes is
new_user_generator GET /users/:user_id/generators/new(.:format) generators#new
When i click
<%= link_to 'GENERATE RAPD PRIMER',user_generator_results_path(#user), id:'new' %></li>
my URL returns
http://localhost:3000/users//generators/new
There's no id return in that URL. How come ??? How can i fix it? I'm using railscast tutorial about Omniauth FB and Identity for User model. I'm trying to associate User with generator.
Make sure you pass user_id param as per routes:
new_user_generator_path(user_id: #user.id)

Error using nested_form gem with empty association

In my rails app, I have two models, a ClientPage and a ContentSection, where ClientPage has_many :content_sections. I'm using the nested_form gem to both models to be edited with the same form. This works fine as long as the ClientPage has at least one ContentSection, but if there are no associated ClientSections, the using nested_form's link_to_add method throws the following NoMethodError:
undefined method `values_at' for nil:NilClass
The form is structured as follows:
<%= nested_form_for page, form_options do |f| %>
# ClientPage fields
# ClientSections
<%= f.link_to_add "Add new section", :content_sections %>
<% end %>
As long as there is at least one ClientSection associated with the page, this works fine. As soon as there isn't, the error is thrown. Removing the link_to_add also stops the error from being thrown. (There's actually a second nested model under ContentSection, and the same issue arises if there are no associated models.)
Not sure what I'm fairly obvious thing I'm missing, but any pointers or suggestions would be greatly appreciated.
Finally worked this out -- the error was due to the fact that I was using the gem in a slightly non-standard way. Within the form, instead of rendering all of the content sections the standard way:
<%= f.fields_for :content_sections do |section_form| %>
# section fields
<% end %>
I put it inside a loop, as I needed the index of each item (which is not stored within the model itself):
<% page.content_sections.each_with_index do |section, index| %>
<%= f.fields_for :content_sections, section do |section_form| %>
# section fields
<% end %>
<% end %>
The issue doing it this way is that the fields_for method does not get called if the association is empty, and as such the gem cannot build the blueprint for the object (which is used to add in the extra item when link_to_add is called).
The solution was to make sure fields_for got called even if the association was empty:
<% if page.content_sections.empty? %>
<%= f.fields_for :content_sections do |section_form| %>
# section fields
<% end %>
<% end %>

Rails sessions stored in database

I have a page that shows a list of events. When the event is clicked it takes you to a login page and it keeps the session id along with the page. When I go to the next page it always sends the last session variable.
<% #events.each do |e| %>
<%= link_to e.event_name, sessions_new_path %>
<%= e.event_start %> - <%= e.event_stop %>
<% session[:event_id] = e.id %>
<%= session[:event_id] %>
<br>
<% end %>
This shows the session[:event_id] is there and it is storing in the variable but when I click on the link it will send the last session[:event_id] of the loop. Any ideas would be helpful. I am looking at option of either passing the variable to the next page or storing it into a database cell, but not sure on how sessions are stored in databases.
Assuming you are using a supported database with ActiveRecord, you can configure your application to store sessions in the database by editing your_app/config/initializers/session_store.rb:
YourApp::Application.config.session_store :active_record_store
make sure to comment out or remove the line:
# YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session'
Further, there are auth gems that handle redirecting users to protected pages after signing in. If you haven't already, look at Devise and/or authlogic. Personally, I prefer Devise.

Ruby on Rails - image_tag links not working in posts

trying to make link to my recent uploaded image
<%= link_to (image_tag (post.image_url(:thumb))), post.image.url(:original), :class => 'postimage' %>
how ever it's not working, at all...
<% #post.image do |image| %>
<%= link_to (image_tag (post.image_url(:thumb))), post.image.url(:original), :class => 'postimage' %>
<% end %>
the fun part is that
<%= #post.image %>
works. but only shows /uploads/post/image/3/eKoh3.jpg
full code here https://gist.github.com/4332533
This line looks wrong to me:
<% #post.image do |image| %>
(btw your gist actually says #post.image.each do |image|, which I'm assuming is what you meant to do above)
If you are mounting an uploader on your Post model's :image attribute, then this makes no sense. A mounted uploader allows you to upload one image, and you can't iterate over it using each.
I'm not sure what you're trying to do. Are you trying to iterate over all the versions? Try post.image.versions.each
Are you trying to upload multiple images? Carrierwave can't help you with that directly. You'll need to create a new model, Image, and mount your uploader there. Your Post will need a line like
has_many :images
And your Image model will need to belong_to :post. You'll also need to figure out how to upload and manage images in that new table.

Rails 3 - routes for admin section

I am building my first admin section in Rails and I am struggling with routing problems.
My routes.rb looks like this:
get "admin/menuh"
get 'admin/welcome'
namespace :admin do
resources :users
resources :menuh
resources :menuv
resources :welcome
end
And my views structure looks like views/admin/users/files. If I will set to url address of browser the url localhost:3000/admin/users/new, so I will get the error message No route matches {:controller=>"users"} (it's in the file views/admin/users/_form.html.erb - this file is generated by scaffold)... so I would like to ask you - where is the problem? Is here anything important, what I am disregarding?
You've set up your form_for like this, I reckon:
<%= form_for #user do |f| %>
Because the route is in a namespace, you need to tell the form that also:
<%= form_for [:admin, #user] do |f| %>
That should help you fix that issue.