Rails Routes.rb -
how rails know how map each function in controller if routes.rb
file specifies:
resources :users
but within userscontroller
have
class userscontroller < applicationcontroller # /users # /users.json def index @users = user.all respond_to |format| format.html # index.html.erb format.json { render :json => @users } end end # /users/1 # /users/1.json def show @user = user.find(params[:id]) respond_to |format| format.html # show.html.erb format.json { render :json => @user } end end # /users/new # /users/new.json def new @user = user.new respond_to |format| format.html # new.html.erb format.json { render :json => @user } end end # /users/1/edit def edit @user = user.find(params[:id]) end # post /users # post /users.json def create @user = user.new(params[:user]) respond_to |format| if @user.save format.html { redirect_to @user, :notice => 'user created.' } format.json { render :json => @user, :status => :created, :location => @user } else format.html { render :action => "new" } format.json { render :json => @user.errors, :status => :unprocessable_entity } end end end # set /users/1 # set /users/1.json def update @user = user.find(params[:id]) respond_to |format| if @user.update_attributes(params[:user]) format.html { redirect_to @user, :notice => 'user updated.' } format.json { head :no_content } else format.html { render :action => "edit" } format.json { render :json => @user.errors, :status => :unprocessable_entity } end end end # delete /users/1 # delete /users/1.json def destroy @user = user.find(params[:id]) @user.destroy respond_to |format| format.html { redirect_to users_url } format.json { head :no_content } end end end
how rails determine instance index function maps
get/users
while create function
maps post/users
?
how resources :users
know map userscontroller
?
rails based around 'convention on configuration' paradigm.
as such, "resources :users" line indicates standard crud methods supported corresponding userscontroller <-- 'users' prefix matches controller, model etc.
ruby-on-rails routes
No comments:
Post a Comment