undefined method session_path when using devise - ruby

I am getting undefined method 'session_path' for #<#<Class:0x9859218>:0x8ebcbd0> when using devise with my custom controller and view, can anybody shed some light on what I have done wrong?
routes:
devise_for :users, :skip => [:sessions]
as :user do
get 'account/login' => 'account#login', :as => :new_user_session
post 'account/login' => 'account#login_user', :as => :user_session
delete 'account/logout' => 'account#logout', :as => :destroy_user_session
end
controller:
AccountController < Devise::SessionsController
def login
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
respond_with(resource, serialize_options(resource))
end
def login_user
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_flashing_format?
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
def logout
signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
set_flash_message :notice, :signed_out if signed_out && is_flashing_format?
yield if block_given?
respond_to_on_destroy
end
view:
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<ul>
<li>
<%= f.label :login, "Email", :class => labels %>
<%= f.email_field :login, :autofocus => true, :class => "wideinput" %>
</li>
<li>
<%= f.label :password, :class => "labels" %>
<%= f.password_field :password, :autocomplete => "off" %>
</li>
<li class='button'>
<%= submit_tag "Login", :class => "btn1" %>
</li>
</ul>
<% end %>

Try to change this line
post 'account/login' => 'account#login_user', :as => :user_session
to
post 'account/login' => 'account#login_user', :as => :session

Related

adding event functionality to my redmine calendar using bitnami stack

I want to add, update and delete event on my redmine calendar(almost similar to a google calendar).I have installed redmine on a Bitnami stack. But I'm not able to understand the code structure as I do not have proper documentation. Is there anyone who can help me on this.
The below listed code is my calendar controller .
class CalendarsController < ApplicationController
menu_item :calendar
before_action :find_optional_project
rescue_from Query::StatementInvalid, :with => :query_statement_invalid
helper :issues
helper :projects
helper :queries
include QueriesHelper
def show
if params[:year] and params[:year].to_i > 1900
#year = params[:year].to_i
if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
#month = params[:month].to_i
end
end
#year ||= User.current.today.year
#month ||= User.current.today.month
#calendar = Redmine::Helpers::Calendar.new(Date.civil(#year, #month, 1), current_language, :month)
retrieve_query
#query.group_by = nil
#query.sort_criteria = nil
if #query.valid?
events = []
events += #query.issues(:include => [:tracker, :assigned_to, :priority],
:conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", #calendar.startdt, #calendar.enddt, #calendar.startdt, #calendar.enddt]
)
events += #query.versions(:conditions => ["effective_date BETWEEN ? AND ?", #calendar.startdt, #calendar.enddt])
#calendar.events = events
end
render :action => 'show', :layout => false if request.xhr?
end
end
The below is the calender_helper.rb
module CalendarsHelper
def link_to_previous_month(year, month, options={})
target_year, target_month = if month == 1
[year - 1, 12]
else
[year, month - 1]
end
name = if target_month == 12
"#{month_name(target_month)} #{target_year}"
else
"#{month_name(target_month)}"
end
# \xc2\xab(utf-8) = «
link_to_month(("\xc2\xab " + name), target_year, target_month, options)
end
def link_to_next_month(year, month, options={})
target_year, target_month = if month == 12
[year + 1, 1]
else
[year, month + 1]
end
name = if target_month == 1
"#{month_name(target_month)} #{target_year}"
else
"#{month_name(target_month)}"
end
# \xc2\xbb(utf-8) = »
link_to_month((name + " \xc2\xbb"), target_year, target_month, options)
end
def link_to_month(link_name, year, month, options={})
link_to(link_name, {:params => request.query_parameters.merge(:year => year, :month => month)}, options)
end
end
Below is the view ,show.html.erb
<h2><%= #query.new_record? ? l(:label_calendar) : #query.name %></h2>
<%= form_tag({:controller => 'calendars', :action => 'show', :project_id => #project},
:method => :get, :id => 'query_form') do %>
<%= hidden_field_tag 'set_filter', '1' %>
<fieldset id="filters" class="collapsible <%= #query.new_record? ? "" : "collapsed" %>">
<legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend>
<div style="<%= #query.new_record? ? "" : "display: none;" %>">
<%= render :partial => 'queries/filters', :locals => {:query => #query} %>
</div>
</fieldset>
<p style="float:right;">
<%= link_to_previous_month(#year, #month, :accesskey => accesskey(:previous)) %> | <%= link_to_next_month(#year, #month, :accesskey => accesskey(:next)) %>
</p>
<p class="buttons">
<%= label_tag('month', l(:label_month)) %>
<%= select_month(#month, :prefix => "month", :discard_type => true) %>
<%= label_tag('year', l(:label_year)) %>
<%= select_year(#year, :prefix => "year", :discard_type => true) %>
<%= link_to_function l(:button_apply), '$("#query_form").submit()', :class => 'icon icon-checked' %>
<%= link_to l(:button_clear), { :project_id => #project, :set_filter => 1 }, :class => 'icon icon-reload' %>
</p>
<% end %>
<%= error_messages_for 'query' %>
<% if #query.valid? %>
<%= render :partial => 'common/calendar', :locals => {:calendar => #calendar} %>
<%= call_hook(:view_calendars_show_bottom, :year => #year, :month => #month, :project => #project, :query => #query) %>
<p class="legend cal">
<span class="starting"><%= l(:text_tip_issue_begin_day) %></span>
<span class="ending"><%= l(:text_tip_issue_end_day) %></span>
<span class="starting ending"><%= l(:text_tip_issue_begin_end_day) %></span>
</p>
<% end %>
<% content_for :sidebar do %>
<%= render :partial => 'issues/sidebar' %>
<% end %>
<% html_title(l(:label_calendar)) -%>
This is the only code related to calendar in routes.rb
get '/projects/:project_id/issues/calendar', :to => 'calendars#show', :as => 'project_calendar'
get '/issues/calendar', :to => 'calendars#show'
In this controller you have only show action (HTTP GET). If you want update and delete you are supposed to add methods and logic for them gere. You will also need routes in config/routes.rb.
I would also recommend you to take a look into other controllers since this one looks more like a renderer of the calendar and nothing more. I don't see create action as well (HTTP POST) which means you create the entity somewhere else and update and delete are supposed to be there as well (if they are not defined already).

Padrino and sequel nested attributes form

I am trying to make a form for storing data hierarchy through the associations.
My structure of database is
meta
- ID (PK)
version
- meta_id (FK of meta)
- ID (PK)
- lang (FK from lang ...)
- ....
data
- ID (FK of version)
I need to store multiple language content under one ID.
Models are
class Meta < Sequel::Model(:link_meta)
one_to_many :version
end
class Version < Sequel::Model(:link_version)
one_to_one :meta, key: :meta_id
one_to_many :data, key: :id
one_to_one :lang, key: :lang
end
class Link < Sequel::Model(:link)
plugin :timestamps, :create => :created, :update => :updated
many_to_one :version, key: :id
end
And my form for creating input is.
<% form_for :links, url(:links, :create), :class => 'form-horizontal' do |f| %>
<%= partial 'links/form', :locals => { :f => f } %>
<% end %>
And this is my partial
<% f.fields_for :version, :class => 'form-horizontal' do |cz| %>
<%= cz.hidden_field :lang, :value => Lang.select(:id).where(:lang => "cz").map(:id) %>
<% cz.fields_for :data, :class => 'form-horizontal' do |link| %>
<% error = #links.errors.key?(:url) && #links.errors[:url].count > 0 %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
... form content
</fieldset>
<% end %>
<% f.fields_for :version, :class => 'form-horizontal' do |en| %>
<%= en.hidden_field :lang, :value => Lang.select(:id).where(:lang => "cz").map(:id) %>
<% en.fields_for :data, :class => 'form-horizontal' do |link| %>
<% error = #links.errors.key?(:url) && #links.errors[:url].count > 0 %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
... another form content
</fieldset>
<% end %>
<% end %>
But it fails on
undefined method `data' for [:class, "form-horizontal"]:Array
I tried different association combinations etc, I feel lost.
Thanks
Levi

How can I solve MassAssignmentSecurity error using rails version 3.2.19

Can anybody please help me to resolve the following error using rails version-3.2.19 ?When i am submitting values to database this error is coming.
Error
ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: con_password
My code snippets are as follows.
views/users/new.html.erb
<center>
<h1>Enter your data</h1>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-div">
<%= form_for :user,:url => {:action => 'create'} do |f|%>
<p>
<%= f.label :Name %>
<%= f.text_field :name,placeholder:"Enter your name" %>
</p>
<p>
<%= f.label :Email %>
<%= f.email_field :email,placeholder:"Enter your Email" %>
</p>
<p>
<%= f.label :Password %>
<%= f.password_field :password,placeholder:"Enter your password" %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :con_password,placeholder:"Enter your password again" %>
</p>
<p>
<%= f.label :content %>
<%= f.text_field :content,placeholder:"Enter your content" %>
</p>
<p>
<%= f.submit "Create User",:class => 'submit' %>
</p>
<% end %>
</div>
</center>
controller/users_controller.rb
class UsersController < ApplicationController
def index
end
def new
#user=User.new
end
def show
end
def create
#user=User.new(params[:user])
if #user.save
flash[:notice]="User has created successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="User could not create"
flash[:color]="invalid"
render :new
end
end
end
model/user.rb
class User < ActiveRecord::Base
attr_accessible :content, :email, :name, :password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
end
migrate\20150128062543_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.string :content
t.timestamps
end
end
end
Please help me to solve this issue.
Instead of <%= f.password_field :con_password,placeholder:"Enter your password again" %>, you should name your field password_confirmation, because this is what your model expects if it has validates :password, confirmation: true:
<%= f.password_field :password_confirmation, placeholder: "Enter your password again" %>
You also have to add password_confirmation to attr_accessible in the model.

How to resolve NoMethodError in Users#edit in ror

Please anyone help me to resolve the following error.Actually I want to show the exit values those present in DB on the text field in edit page.But i am getting some error.
Error:
NoMethodError in Users#edit
Showing C:/Site/edit/app/views/users/edit.html.erb where line #4 raised:
undefined method `each' for #<User:0x3ac9ea8>
Below are my code snippets
views/users/index.html.erb:
<h1>This is index page</h1>
<center>
<p>Enter data</p>
<div class="option">
<p><%= link_to "Click here to enter data",users_new_path %></p>
<p><%= link_to "Display data",users_show_path%></p>
</div>
</center>
views/users/show.html.erb
<h1>Show your data</h1>
<center>
<ul>
<% #user.each do |t| %>
<li>
<%= t.name %> |
<%= t.email %> |
<%= t.password%> |
<%= t.created_at %>
<%= link_to "edit",users_edit_path(:id => t.id) %> || <%= link_to "Reset Password",users_reset_path(:id => t.id) %>
</li>
<% end %>
</ul>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</center>
views/users/edit.html.erb
<h1>Edit your data here</h1>
<center>
<%= form_for #user ,:url => {:action => "update",:id => params[:id]} do |f| %>
<% #edit.each do |t| %>
<div class="div_reg">
<p>
<label for="username" class="uname" data-icon="u" >username </label>
<%= f.text_field:name, :input_html => {:value => t.name}%>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Email </label>
<%= f.text_field:email,:input_html => {:value => t.email} %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field:password,:input_html => {:value => t.password} %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field :password_confirmation %>
</p>
<center>
<%= f.submit "Update",:class => 'btn-custom' %>
</center>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</div>
<% end %>
<% end %>
</center>
<% if #user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in #user.errors.full_messages %>
<li><%= message_error %></li>
<% end %>
</ul>
<% end %>
controller/users_controller.rb
class UsersController < ApplicationController
def index
end
def new
#user=User.new
end
def create
#user=User.new(users_param);
if #user.save
flash[:notice]="You signed up successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You have not signed up successfully"
flash[:color]="invalid"
redirect_to :action => 'new'
end
end
def show
#user=User.all
end
def edit
#user=User.new
#edit=User.find(params[:id])
end
def update
flash[:notice]=params[:id]
#user=User.find(params[:id])
if #user.update_attributes(update_params)
flash[:notice]="Your data is updated succesfully"
flash[:color]="valid"
redirect_to :action => 'show'
else
flash[:alert]="Your data could not update,Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'edit'
end
end
def reset
#user=User.new
end
def emailsend
#user=User.find(params[:id])
if #user.email== params[:user][:email]
UserMailer.registration_confirmation(#user).deliver
flash[:notice]="Check your email to reset the password"
flash[:color]="valid"
redirect_to :action => 'reset'
else
flash[:notice]="Check your valid email or your email is not found"
flash[:color]="invalid"
redirect_to :action => 'show'
end
end
def resetpass
#user=User.new
end
def passres
#user=User.find_by_email(params[:user][:email])
if #user.update_attributes(updates_password)
flash[:notice]="Your password id updated succefully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Your data could not update..Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'show'
end
end
private
def users_param
params.require(:user).permit(:name, :email, :password,:password_confirmation)
end
def update_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
def updates_password
params.require(:user).permit(:email,:password,:password_confirmation)
end
end
model/user.rb
class User < ActiveRecord::Base
#attr_accessor :password
#attr_accessor :name
#attr_accessor :email
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
end
Please help me to resolve this error.Thanks in advance.
#edit.each is your problem. You have a User instance here not an ActiveRecord array of Users therefore .each isn't available.
You can remove the iteration <% #edit.each do |t| %> and change all references t to #edit ie. #edit.name.

Rails form_for own action routes

I want to modify the action (submit) for a form_for helper
<%= form_for(#rating, :as => :post, :url => demo_create_rating_path(#rating)) do |f| %>
<div class="field">
<%= f.label :value %><br />
<%= f.select :value, %w(1 2 3 4 5) %>
</div>
<%= f.hidden_field :article_id, :value => #article.id%>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, size: "100x5" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my View and it does not work.
All I want is, that I can redirekt the Action after submit button but then a get this error:
ActionController::RoutingError (No route matches {:controller=>"demo_ratings", :action=>"create", :article_id=>#<Rating id: nil, value: nil, description: nil, article_id: nil, user_id: nil, created_at: nil, updated_at: nil>}):
app/views/demo_ratings/_form.html.erb:1:in `_app_views_demo_ratings__form_html_erb__1912848844925280312_70155649546120'
app/views/demo_ratings/new.html.erb:13:in `_app_views_demo_ratings_new_html_erb__27525029454473720_70155632487040'
What am I doing wrong?
UPDATE
All my funktion that are need by form_for helper:
def new
#rating = Rating.new
#article = Article.find(params[:article_id])
end
def edit
#rating = Rating.find(params[:id])
#article = Article.find(params[:article_id])
end
def create
#rating = Rating.new(params[:rating])
if #rating.save
#article= Article.find(params[:article_id])
puts #article.name
puts #rating.id
#rating.article = #article
puts #rating.article.name
redirect_to demo_rating_path(#rating, :article_id => #article.id), notice: 'Rating was successfully created.'
else
render action: "new"
end
end
def update
#rating = Rating.find(params[:id])
if #rating.update_attributes(params[:rating])
#article = #rating.article
redirect_to demo_rating_path(#rating), notice: 'Rating was successfully updated.'
else
render action: "edit"
end
end
Try this:
<%= form_for(#rating, :as => :post, :url => demo_create_rating_path) do |f| %>
The #rating in the url is providing a nil object id, and you don't have an id yet.
If you want to share the form between create and update, then use the following:
<% form_for(#rating, :as => :post) do |f| %>
For reference, review the output of a rails generated scaffold's _form.html.erb.
In your controller, your are saving the new/ updated record before your processing. The statement if #rating.save should come after #rating.article = #article.
def create
#rating = Rating.new(params[:post])
#article= Article.find(params[:article_id])
#rating.article_id = #article.id
if #rating.save
redirect_to demo_rating_path(#rating, :article_id => #article.id), notice: 'Rating was successfully created.'
else
render action: "new"
end
end

Resources