Rails 5 - how to establish routes for namespaced models -


i trying learn how use namespaced models in rails 5 app better organise content.

i have address model. polymorphic. each of settings , organisation have many addresses. settings model namespaced under user.

the associations are

user

has_one :setting, dependent: :destroy  

setting

belongs_to :user    has_many :addresses, as: :addressable#, class_name: address     accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true 

organisation

has_many :addresses, as: :addressable#, class_name: address     accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true 

address

belongs_to :addressable, :polymorphic => true, optional: true 

routes - organisation

resources :organisations     namespace :contacts       resources :addresses       resources :phones      end   end 

routes - setting

resources :users, shallow: true     scope module: :users       resources :identities       resources :settings          namespace :contacts           resources :addresses           resources :phones          end       end      end   end  

organisation form

<%= f.simple_fields_for :addresses |f| %>       <%= f.error_notification %>         <%= render 'contacts/addresses/address_fields', f: f %>      <% end %>        <%= link_to_add_association 'add address', f, :addresses, partial: 'contacts/addresses/address_fields' %>  

user / setting form

<%= simple_form_for [@user, @setting] |f| %>   <%= f.error_notification %>      <%= simple_fields_for :addresses |f| %>             <%= f.error_notification %>                 <%= render 'contacts/addresses/address_fields', f: f %>             <% end %>                    <%= link_to_add_association 'manage address', f, :addresses, partial: 'contacts/addresses/address_fields' %>        </div> <% end %>   

everything address functionality works fine organisation, have problem in getting work settings.

the problem have when try use user/setting form add address error says:

actioncontroller::routingerror @ /contacts/addresses/1/edit uninitialized constant users::contacts 

there isn't direct association between user , contacts. contacts name of namespaced folder use store address views , controller.

can see need in order able access address form functionality user settings form?

when rake routes setting, can see path format settings address, can't figure out how use it.

rake routes | grep setting            setting_contacts_addresses      /settings/:setting_id/contacts/addresses(.:format)                      users/contacts/addresses#index                                       post     /settings/:setting_id/contacts/addresses(.:format)                      users/contacts/addresses#create          new_setting_contacts_address      /settings/:setting_id/contacts/addresses/new(.:format)                  users/contacts/addresses#new 

settings controller

class users::settingscontroller < applicationcontroller     before_action :set_setting, only: [:show, :edit, :update, :destroy]   before_action :authenticate_user!   after_action :verify_authorized    def index     @settings = setting.all     authorize @settings    end    def show     # authorize @setting      @addresses = @setting.addresses.all      @phones = @setting.phones    end     def new     @setting = setting.new     @setting.addresses_build      @setting.phones_build     authorize @setting    end    def edit      @setting.addresses_build unless @setting.addresses      @setting.phones_build unless @setting.phones    end    def create     @setting = setting.new(setting_params)     authorize @setting      respond_to |format|       if @setting.save         format.html { redirect_to @setting }         format.json { render :show, status: :created, location: @setting }       else         format.html { render :new }         format.json { render json: @setting.errors, status: :unprocessable_entity }       end     end   end    def update    respond_to |format|       if @setting.update(setting_params)         format.html { redirect_to @setting }         format.json { render :show, status: :ok, location: @setting }       else         format.html { render :edit }         format.json { render json: @setting.errors, status: :unprocessable_entity }       end     end   end    def destroy     @setting.destroy     respond_to |format|       format.html { redirect_to settings_url }       format.json { head :no_content }     end   end     private     # use callbacks share common setup or constraints between actions.     def set_setting       @setting = setting.find(params[:id])       authorize @setting     end      # never trust parameters scary internet, allow white list through.     def setting_params       params.require(:setting).permit( :newsletter,         addresses_attributes: [:id, :description, :unit, :building, :street_number, :street, :city, :region, :zip, :country, :time_zone, :latitude, :longitude, :_destroy],         phones_attributes: [:phone_number,  :country, :phone_type],         )     end  end 

organisations controller

class organisationscontroller < applicationcontroller   before_action :set_organisation, only: [:show, :edit, :update, :destroy]    def index     @organisations = organisation.all     authorize @organisations   end    def show     @addresses = @organisation.addresses.all      # @hash = gmaps4rails.build_markers(@addresses) |address, marker|     #     marker.lat address.latitude     #     marker.lng address.longitude     #     marker.infowindow address.full_address     # end     @bips = @organisation.bips     @proposals = @organisation.proposals#.in_state(:publish_openly)       end    def new     @organisation = organisation.new     @organisation.addresses#_build   end    def edit     @organisation.addresses_build unless @organisation.addresses   end    def create     @organisation = organisation.new(organisation_params)      respond_to |format|       if @organisation.save         format.html { redirect_to @organisation, notice: 'organisation created.' }         format.json { render :show, status: :created, location: @organisation }       else         format.html { render :new }         format.json { render json: @organisation.errors, status: :unprocessable_entity }       end     end   end    def update     respond_to |format|       if @organisation.update(organisation_params)         format.html { redirect_to @organisation, notice: 'organisation updated.' }         format.json { render :show, status: :ok, location: @organisation }       else         format.html { render :edit }         format.json { render json: @organisation.errors, status: :unprocessable_entity }       end     end   end    def destroy     @organisation.destroy     respond_to |format|       format.html { redirect_to organisations_url, notice: 'organisation destroyed.' }       format.json { head :no_content }     end   end     private     # use callbacks share common setup or constraints between actions.     def set_organisation       @organisation = organisation.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def organisation_params       params.fetch(:organisation, {}).permit(:title, :comment,         addresses_attributes:   [:id, :description, :unit, :building, :street_number, :street, :city, :region, :zip, :country, :time_zone, :latitude, :longitude, :_destroy],         phones_attributes:      [:id, :phone_number,  :country, :phone_type, :_destroy]         )     end   end 

i don't quite understand you're trying accomplish. these codes below allow create new setting user. can add more addresses setting. if want edit address'name, have go edit page of setting address belongs to.

routes.rb

resources :users, shallow: true   scope module: :users     resources :settings   end end 

users/settings_controller.rb

class users::settingscontroller < applicationcontroller   before_action :prepare_user, only: [:index, :new, :create]   before_action :prepare_setting, only: [:show, :edit, :update]    def new     @setting = setting.new   end    def create     @setting = @user.build_setting(setting_params)     if @setting.save       redirect_to @setting     else       render 'new'     end   end    def show   end    def edit   end    def update     if @setting.update(setting_params)       redirect_to @setting     else       render 'edit'     end   end    private    def prepare_user     @user = user.find(params[:user_id])   end    def prepare_setting     @setting = setting.find(params[:id])   end    def setting_params     params.require(:setting).permit(:name, addresses_attributes: [:name, :id])   end end 

users/settings/new.html.erb

<h1>new setting</h1> <%= render 'form' %> 

users/settings/_form.html.erb

<%= simple_form_for [@user, @setting] |f| %>   <%= f.input :name %>   <div>     <%= f.simple_fields_for :addresses |f| %>       <%= f.error_notification %>         <%= render 'contacts/addresses/address_fields', f: f %>     <% end %>     <%= link_to_add_association 'add address', f, :addresses, partial: 'contacts/addresses/address_fields' %>   </div>   <%= f.submit %> <% end %> 

contacts/addresses/address_fields.html.erb

<%= f.input :name, label: 'address name' %> <%= f.input :id, as: :hidden %> 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -