fureelmusic
fureelmusic

Reputation: 23

Ruby On Rails: Create stopped working but Update still works

I recently added an admin user to my page and now my 'create' function no longer works, but my 'edit' function still does. I get no error when I try to create a new item.

application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery

before_filter :is_admin, only: [:new,  :create, :edit, :update, :delete, :destroy, :admin]

helper_method :admin?

protected

  def admin?
  session[:password] == 'pass'
  end


def authorize
  unless admin?
    flash[:error] = "unauthorized access"
    redirect_to root_path
    false
  end
 end

def is_admin
      redirect_to root_path, notice: "You are not authorized for that." unless admin?
    end

end

If I take out the before_filter, it still doesn't work.

members_controller.rb

  def new
    @member = Member.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @member }
    end
  end

  def edit
    @member = Member.find(params[:id])
  end

  def create
    @member = Member.new(params[:member])

    respond_to do |format|
      if @member.save
        format.html { redirect_to @member, notice: 'Member was successfully created.' }
        format.json { render json: @member, status: :created, location: @member }
      else
        format.html { render action: "new" }
        format.json { render json: @member.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @member = Member.find(params[:id])

    respond_to do |format|
      if @member.update_attributes(params[:member])
        format.html { redirect_to @member, notice: 'Member was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @member.errors, status: :unprocessable_entity }
      end
    end
  end

In my routes.rb I have resources :members

Any ideas why my create suddenly quit on me but all the other functions are still working? It is like this for all of my pages, not just 'members'

Upvotes: 0

Views: 137

Answers (3)

fureelmusic
fureelmusic

Reputation: 23

I solved the problem by moving all of my resources in my routes.rb file to the top of the file before everything else. I'm not entirely sure why this was the problem, but everything is working now.

Upvotes: 0

Andrew Kuklewicz
Andrew Kuklewicz

Reputation: 10701

Ah - try this in irb:

> "123" unless true
=> nil

See, when the user is an admin, it returns nil, which is interpreted as false, and the filter stops further execution. You need it to do something like this:

def is_admin
  admin? || redirect_to(root_path, notice: "You are not authorized for that.")
end

Upvotes: 0

kasper375
kasper375

Reputation: 171

Can you show your is_admin function? May be it return false or nil when you create member and true ect. when you update member.

P.S. Don't create methods with name like is_admin in Ruby. It is better to call it admin? ;)

Upvotes: 1

Related Questions