Rails - Couldn't find User with id=edit - ruby-on-rails

I am using Rails 3 and Devise for authentication. When I try to update my current_user, an exception is thrown stating:
Couldn't find User with id=edit
app/controllers/users_controller.rb:49:in `update'
Here is update and edit in UsersController:
#UsersController.rb
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def edit
#user = User.find(params[:id])
end
And here is /views/layouts/_navigation.html.erb:
<%= link_to "Home", root_path, :class => 'brand' %>
<ul class="nav">
<% if user_signed_in? %>
<li>
<%= link_to('Logout', destroy_user_session_path, :method=>'delete') %>
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
<% end %>
<li>
<%= link_to('About', help_about_path) %>
</li>
<% if user_signed_in? %>
<li>
<%= link_to('Edit Account', edit_user_path(current_user)) %>
</li>
<% end %>
<% if user_signed_in? and current_user.role == "gen_admin" || current_user.role == "teacher" %>
<li>
<%= link_to('View Students', users_path ) %>
</li>
<% end %>
<% if user_signed_in? and current_user.role == "gen_admin" || current_user.role == "teacher" %>
<li>
<%= link_to('New User', new_user_path ) %>
</li>
<% end %>
<% if user_signed_in? %>
<li>
<%= link_to('Student Data', data_path) %>
</li>
<% end %>
</ul>
Finally, here is /views/devise/registrations/edit.html.erb:
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, :as => resource_name, :url => edit_user_registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :teacher %><br />
<%= f.text_field :teacher %></div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div><%= f.label :new_password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
<%= link_to "Back", :back %>
Why is the user id=edit? Here is my hash, if that helps:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"7g1WsNCVuNY/5ZTeBUdv97tbdAPacvvDAzBSMGCcuNY=",
"user"=>{"email"=>"email#admin.com",
"teacher"=>"Demont",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"current_password"=>"[FILTERED]"},
"commit"=>"Update",
"id"=>"edit",
"format"=>"user"}

Try using:
#user = current_user
Something looks to be wrong with your route or edit form. Try using the form like in their wiki page...
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password
But the above code should get around this

Most likely you I've got conflict in your routes when added UsersController.
There is devise's wiki page explaining that.
Default devise's route for edit user is /users/edit but in your controller path for update action is /users/:id. What's why you've got "edit" instead of user_id

Use
<%= form_for(#resource, :as => resource_name, :url => edit_user_registration_path(resource_name), :html => { :method => :put }) do |f| %>
to your form view and
#resource = User.find(params[:id])
in your edit action.

Related

Rerouting to path upon submit rails

I'm giving the user the ability to reset their password and then reroute upon completion. However when using form tags and redirect_to, I'm getting some undesired rerouting.
Upon successful submission, the form should reroute to:
https://myherokuapp.herokuapp.com/customer_password_resets/new
but instead it reroute to:
https://myherokuapp/customer_password_resets/create.ShoyTPeAC5UAEjjWwZh_HA //the extra piece is the token id
CustomerPasswordResetsController Controller
def edit
#customer = Customer.find_by_password_reset_token!(params[:id])
end
def update
#customer = Customer.find_by_password_reset_token!(params[:id])
if #customer.password_reset_sent_at < 2.hours.ago
redirect_to new_customer_password_reset_path, :alert => "Password reset has expired."
elsif #customer.update_attributes(params[:customer])
redirect_to new_customer_password_reset_path, :notice => "Password has been reset!"
else
render :edit
end
end
customer_password_resets/edit.html.erb
<%= form_for #customer, :url => customer_password_resets_path(params[:id]) do |f| %>
<% if #customer.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #customer.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Update Password" %></div>
The url option is the problem there. Rails will submit alright. The new view will submit to a create action and the edit to the update
Update, the previous answer did sucesfully reroute but did not update. I need the params in order to find the user in the update model. Rake routes showed that I indeed needed to change this from post to patch !
<%= form_for #customer, :url => customer_password_resets_path, :method => :patch do |f| %>

How do I re-populate form fields when validation fails?

This is the erb template:
<div id='recipe-form'>
<% if #recipe.errors %>
<div id='errors'>
<% #recipe.errors.messages.each do |field, messages| %>
<div class='error'>
<div class=field'><%= field %></div>
<div class='messages'>
<ul>
<% messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
</div>
<% end %>
<%= form_for #recipe, :html => {:multipart => true}, :url => '/recipes' do |f| %>
<%= f.label :title, 'title' %>
<%= f.text_field :title %>
<div id="photo-upload">
<%= file_field :photo0, :image, :id => 0 %>
</div>
<div id='existing-photos'>
<% recipe.photos.each do |photo| %>
<div id='<%= photo.id %>'>
<img src='<%= photo.image.url(:thumb) %>' />
<ul>
<li>
<%= link_to 'delete',
recipe_photo_url(
:recipe_id => #recipe.slug,
:id => photo.id
),
:method => :delete,
:remote => true
%>
</li>
</ul>
</div>
<% end %>
</div>
<%= f.label :body, 'body' %>
<%= f.cktext_area :body, :ckeditor => {:width => "500"} %>
<%= f.label :tags, 'tags (comma separated)' %>
<%= text_field_tag :tags %>
<%= submit_tag 'submit' %>
<% end %>
</div>
This is the create action:
def create
#recipe = Recipe.new(params[:recipe])
photo_keys = params.keys.select{|k|k.match(/^photo/)}
#photos = []
photo_keys.each do |photo_key|
#photos << Photo.new(params[photo_key])
end
#recipe.tags = Tag.parse(params[:tags])
#recipe.author = current_user
if #recipe.save &&
#photos.all?{|photo|photo.save}
#photos.each do |photo|
photo.recipe_id = #recipe.id
photo.save
end
flash[:notice] = 'Recipe was successfully created.'
redirect_to recipe_url(#recipe.slug)
else
flash[:error] = 'Could not create recipe. '
flash[:error] += 'Please correct any mistakes below.'
render :action => :new
end
end
And this is the new action:
def new
#recipe = Recipe.new
end
I read that if I use form_for as I am using above, the fields will be re-populated automatically.
When I inspect #recipe.errors from within the erb template, I can see that the errors generated by create are also available when the new action is rendered, but the fields do not re-populate.
I'm actually not sure about what render action: does but what I do and works is: Instead of rendering the action just render the template using render :new.
You need to set the same instance variables (those with #), which you already in your create action.

Rails - AJAX/JQuery not working

For my application, I have projects where users can make comment (class Newcomments) postings. Right now it posts great but on page refresh. I am trying to use AJAX/JQUERY so that it will post with no page refresh. I am following this railscasts tutorial.
So right now, the posts are made to my database and the page does not refresh. But when I refresh, the posts shows up. The page that I render my comments for a specific project is on the projects/_comments.html.erb.
Question: How would I adjust it so that it the new comment made renders?
newcomments_controller.rb
def create
#newcomment = #commentable.newcomments.new(params[:newcomment])
if #newcomment.save
respond_to do |format|
format.html { redirect_to comments_project_path(#commentable) }
format.js
end
else
render :new
end
end
view/newcomments/_form.html.erb
<span class="comment">
<%= form_for [#commentable, #newcomment], remote: true do |f| %>
<%= f.text_area :content, rows: 3, :class => "span8" %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit "Add Comment", :class => "btn btn-header" %>
<% end %>
</span>
view/newcomments/create.js.erb
$('#newcomment').append('<%= j render(#newcomments) %>');
projects_controller.rb
def comments
#commentable = #project
#newcomments = #commentable.newcomments.newest.page(params[:comments_page]).per_page(10)
#newcomment = Newcomment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: #project.comments }
end
end
projects/comments.html.erb
<%= render 'comments' %>
projects/_comments.html.erb
<%= render #newcomments %>
view/newcomments/_newcomment.html.erb
<div class="comments">
<%= link_to newcomment.user.name %></strong>
Posted <%= time_ago_in_words(newcomment.created_at) %> ago
<%= newcomment.content %>
</div>
<span class="comment">
<%= form_for [#commentable, #newcomment] do |f| %>
<div class="field">
<%= f.text_area :content, rows: 3, :class => "span8" %>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= f.submit "Add Comment", :class => "btn btn-header" %>
</div>
<% end %>
<% unless newcomment.newcomments.empty? %>
<%= render #newcomments %>
<% end %>
</span>
Try binding AJAX actions as described here:
http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
Also, consider returning a render comments partial html instead of json comment object, to do so you need to tell in form after :remote directive that :'data-type'=>'html', so that returned html will hit function binded on AJAX success, and then swap html of container div with, for example, jQuery

Ruby - Authenticate.. Login Problems

Hi I would like to create a authentication with Ruby on Rails.
I have no Error - Message but i cant login.
on my localhost this picture appears ( sorry i cant post a screen )
My Workingtimes
C 2011 | login
The Login - Button dont works.
Here are my code:
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Mytimes</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<div id="header">
<h1> My Workingtimes </h1>
</div>
<div id="content">
<% if flash[:notice] %>
<p id="notice">
<%= flash[:notice] %>
</p>
<% end %>
<% if flash[:alert] %>
<p id= "alert">
<%= flash[:alert] %>
</p>
<% end %>
</div>
<div id="footer">
© 2012 |
<% if user_signed_in? %>
<%= link_to "logout", logout_path, method: :delete %>
<% else %>
<%= link_to "login", login_path %>
<% end %>
</div>
</div>
</body>
</html>
routes.rb
Mytimes::Application.routes.draw do
resources :mytimes
resources :users, :only => [:new, :create]
resources :sessions, :only => [:create]
get "login" => "sessions#new", as: "login"
post "sessions" => "sessions#create", as: "sessions"
delete "logout" => "sessions#destroy", as: "logout"
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def current_user
if session[:user_id]
#current_user ||= User.find(session[:user_id])
end
end
def user_signed_in?
current_user.present?
end
helper_method :user_signed_in?
end
session_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to mytimes_path,
notice: "Your are signed!"
else
flash.now.alert = "Failed Email or Password!"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to mytimes_path,
notice: "You are logged out!"
end
end
sessions/new.html.erb
<h2> Log In </h2>
<%= form_tag sessions_path do %>
<p>
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</p>
<p>
<%= label_tag :password %>
<%= password_field_tag :password %>
</p>
<p><%= submit_tag "Log In" %></p>
<% end %>
Iam thankful for your help.
users/ new.html.erb
<h2>New User</h2>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</p>
<p><%= f.submit %></p>
<% end %>
There is no yield in your application.html.erb. The views will therefore not be displayed.

How to populate form in nested model when error occurs?

Running Rails 3.2.1.
I went through the "Getting started" guide on the Ruby on Rails site. I have set up a blog post where someone can comment on the posts.
I modified the example to show an error when I enter in comments that don't validate (no name or comment text).
(A post has multiple comments, etc.)
However, how do I have Rails put this problematic comment back in the form, instead of the page?
Here is my create method in the comments controller:
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(params[:comment])
respond_to do |format|
if #comment.save
format.html { redirect_to(#post, :notice => 'Comment was successfully created.') }
else
format.html { render 'posts/show' }
end
end
end
Here is my show.html.erb from posts:
<%= render #post %>
<h3>Comments</h3>
<%= render #post.comments %>
<h3>Add a comment</h3>
<%= render "comments/form" %>
<p>
<% if user_signed_in? %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
</p>
And here is my comment form partial:
<%= form_for([#post, #post.comments.build]) do |f| %>
<%= render "shared/error_messages", :target => #comment %>
<div class="field">
<%= f.label "Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit "Add Comment!" %>
</div>
<% end %>
And here's my comment partial:
<p>
<strong><%= comment.name %></strong><br />
<%= comment.created_at.try(:strftime, "on %A, %B %d %Y at %H:%M:%S") %><br />
<%= simple_format(h(comment.body), :sanitize => true) %>
</p>
In your form when you return from an error.
<% if #comment.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

Resources