I am trying to create crud in rails. I think my routes in the namespace aren't working fine. When I try to create new record(country) it redirects me to index action when the request should go the create action on POST /admin/countries
Following is the code:
Controller:
class Admin::CountriesController < ApplicationController
layout "admin"
def index
@countries = Country.all
end
def show
@country = Country.find(params[:id])
end
def new
@country = Country.new
end
def edit
@country = Country.find(params[:id])
end
def create
abort("Message goes here")
@country = Country.new(country_params)
if @country.save
redirect_to @country
else
render 'new'
end
end
def update
@country = Country.find(params[:id])
if @country.update(country_params)
redirect_to @country
else
render 'edit'
end
end
def destroy
@country = Country.find(params[:id])
@country.destroy
redirect_to countries_path
end
private
def country_params
params.require(:country).permit(:name, :status)
end
end
Action-View (new)
<%= form_for [:admin, @country] do |f| %>
<div class="box-body">
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, :class => 'form-control', :placeholder => 'Country name' %>
</div>
<div class="checkbox">
<label>
<%= f.check_box :status %> Is enabled?
</label>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<%= f.submit :class => "btn btn-primary" %>
</div>
<% end %>
Routes
Rails.application.routes.draw do
root "auth#login"
get 'shop', :to => "auth#index"
match ':controller(/:action(/:id))', :via => [:get,:post]
match ':controller(/:action(/:id))', :via => [:get,:post], controller: /admin\/[^\/]+/
namespace :admin do
# root "auth#login"
resources :countries
end
end
Aucun commentaire:
Enregistrer un commentaire