2016-03-05 81 views
0

問題:當我訪問create.save時,出現錯誤ActionController::UnknownFormat in GuestsController#create,通過記錄保存。任何想法如何擺脫它?用CRUD設計模型

我:

  • rails g devise guest;
  • rails g controller guests適用於CRUD界面。

guests_controller.rb:

class GuestsController < ApplicationController 
    before_action :set_guest, only: [:show, :edit, :update, :destroy] 
    def new 
    @guest = Guest.new 
    end 
    def edit 
    @guest = Guest.find(params[:id]) 
    end 
    def create 
    respond_to do |format| 
     @guest = Guest.new(guest_params) 
     if @guest.save 
     redirect_to guests_path, notice: 'Client was successfully created.' 
     else 
     render :new 
     end 
    end 
    end 

    def update 
    #update without changing password 
    if params[:guest][:password].blank? 
     params[:guest].delete(:password) 
     params[:guest].delete(:password_confirmation) 
    end 
    #usual actions 
    @guest = Guest.find(params[:id]) 
    if @guest.update_attributes(guest_params) 
     sign_in(@guest, :bypass => true) if @guest == current_guest 
     redirect_to guests_path, notice: 'Client was successfully updated.' 
    else 
     render :edit 
    end 
    end 

    private 
    def set_guest 
     @guest = Guest.find(params[:id]) 
    end 
    def guest_params 
     params.require(:guest).permit(:email, :password, :password_confirmation) 
    end 
end 

可能有一些在註冊/控制器重定向...

我試過路線devise_for :guests, controllers: { registrations: 'guest_registrations' } + guest_registrations_controller.rb

class GuestRegistrationsController < Devise::RegistrationsController 
    protected 

    def after_sign_up_path_for(guest) 
    guests_path # Or :prefix_to_your_route 
    end 
end 

,但什麼也沒做

回答

1

您正在使用respond_to但你說的不是響應格式

試試這個:

def create 
    respond_to do |format| 
     format.html do 
     @guest = Guest.new(guest_params) 
     if @guest.save 
      redirect_to guests_path, notice: 'Client was successfully created.' 
     else 
      render :new 
     end 
     end 
    end 
end 

的響應格式可以:json, :html, :xml, :js

更多更好的版本比上面:

def create 
    respond_to do |format| 
     @guest = Guest.new(guest_params) 
     if @guest.save 
      format.html { redirect_to guests_path, notice: 'Client was successfully created' } 
      format.json {render json: @guest} 
     else 
      format.html { render :new } 
      format.json { render json: @guest.errors.full_messages, status: :bad_request } 
     end 
    end 
end 
+0

它的工作:)我想我有相同的添加到'update'行動。謝謝,Rahul –

+0

是的,如果你要處理多個響應格式。 –