2 Replies - 1979 Views - Last Post: 04 January 2013 - 09:44 AM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

No route matches {:action=>"edit", :controller=>"*

Posted 04 January 2013 - 08:36 AM

*SOLVED*


In honor of the new vulnerability patch for all versions of rails, I've decided to upgrade an app of mine from 3.0 to 3.1 and then 3.2. I'm having a problem with my users#show page no longer working. It seems that the problem code has something to do with the Person model I created a long time ago (I wanted each user to be able to have multiple persons on their account).

(view.html.erb)
		<% @persons.each do |person| %>
		  <tr>
                    .
                    .
                    .
		    <td><%= link_to 'Edit', edit_person_path(person) %></td>
		  </tr>
		<% end %> 



(`rake routes` output)
.
.
.
{:action=>"edit", :controller=>"persons"}
                                          person GET    /persons/:id(.:format) 
.
.
.




(routes.rb)
  resources :persons



(config/initializers/inflections.rb)
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'person', 'persons'
end



(persons_controller.rb)
class PersonsController < ApplicationController
  skip_before_filter :authenticate
  
  def new
    @person = Person.new #last
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @news_blast }
    end
  end
  
  def create
    #@person = Person.new(params[:person])
    @user = User.where(:id => params[:person][:user_id] ).first
    
    #make sure the user being edited is the current user
    if current_user != @user
      flash[:error] = "You don't have access to this user's persons"
      redirect_to "/"
      return
    end
    
    if current_user.has_password?(params[:password])  # if they had the password correct...
      current_user.password = params[:password]  # then set the password in current_user... i guess...
    else
      flash[:error] = "You had the wrong password."  # or die
      redirect_to @user
      return
    end
    
    current_user.persons.create(params[:person])
    
    if current_user.save # current_user.update_attributes(:person => params[:person])
      # sign_in @user
      flash[:success] = "Person successfully created!"
      
      redirect_to @user
    else
      flash[:error] = current_user.errors
      redirect_to @user
    end
  end

  
  # DELETE /persons/1
  # DELETE /persons/1.xml
  def destroy
    @person = Person.find(params[:id])
    @user = User.find(@person.user_id)  # find it's parent so we know where to redirect to
    @person.destroy
    
    flash[:success] = "Person removed from account."
    
    respond_to do |format|
      format.html { redirect_to @user } # redirect_to(persons_url) }
      format.xml  { head :ok }
    end
  end
  
  def edit
    @person = Person.find(params[:id])
  end

  # PUT
  def update
    @person = Person.find(params[:id])

    respond_to do |format|
      if @person.update_attributes(params[:todo])
        format.html { redirect_to(@person, :notice => 'Person was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
      end
    end
  end
end


This post has been edited by NotarySojac: 04 January 2013 - 09:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: No route matches {:action=>"edit", :controller=>"*

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: No route matches {:action=>"edit", :controller=>"*

Posted 04 January 2013 - 09:14 AM

Fixed it.

Ok, so I gave my routing problem a rest and started playing around with a visual bug that was completely unrelated... Or so I thought. It turned out that I was creating a new person in my controller so I could add it on that same page that was indexing them all. But this blank person was getting run through the <% @persons.each do |person| loop and showing up on the rendered view as a person with all blank values, and aparently something changed between rails 3.0 and 3.1 where this nil person's edit page link was throwing an exception.

After adding a next if person.id.nil? things are working as they did in version 3.0.x

This post has been edited by NotarySojac: 04 January 2013 - 09:16 AM

Was This Post Helpful? 1
  • +
  • -

#3 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 120
  • View blog
  • Posts: 1,681
  • Joined: 28-June 08

Re: No route matches {:action=>"edit", :controller=>"*

Posted 04 January 2013 - 09:44 AM

Hey, I just wanted to say, kudos for replying with the answer to your own question so that others in the future can benefit from it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1