簡體   English   中英

ActionController :: UnknownFormat錯誤

[英]ActionController::UnknownFormat error

我想從控制器中的方法調用JavaScript函數,該函數以方法create.js.erb的名稱寫入文件中,但出現錯誤ActionController :: UnknownFormat。我不知道這是什么問題?

app / views / favorite_places / create.js.erb

function my_function()
{
    swal("Place is not saved in google maps!", "Please move the marker to the desired location and add its name");
}

應用程序/控制器/ favorite_places_controler

def create
        #Checks if the current user have this favorite place already ,it renders Favorite Place already exists
        if current_user.favorite_places.include?(FavoritePlace.find_by(:name => favorite_place_params[:name]))
          id=FavoritePlace.find_by(:name => favorite_place_params[:name]).id
          redirect_to favorite_places_path , notice: 'Favorite place already exists'
        else
         #Checks if the favorite Place exists in the database it finds the place puts it in the variable favorite place
          if FavoritePlace.exists?(:name => favorite_place_params[:name])
            @favorite_place = FavoritePlace.find_by(:name => favorite_place_params[:name])
         #Or it  will create a new one with the allowed parameters only.  
          else
            @favorite_place = FavoritePlace.new(favorite_place_params)
            @favorite_place.save
          end
          id=@favorite_place.id
          #It assigns the favorite place to the user.
          UserFavoritePlace.add_favorite_place(current_user,@favorite_place) 
          redirect_to favorite_places_path , notice: 'Favorite place was successfully added.'
          respond_to do |format|
            format.js { render :js => "my_function();" }
          end
        end
      end

您在react_to塊之前有一個redirect_to,這是錯誤的-redirect_to應該位於response_to塊內的format.html塊中。

您未知的格式錯誤可能是由於控制器嘗試處理和html請求而引起的,而您沒有在respond_to塊內處理該請求。 嘗試改變這個

      redirect_to favorite_places_path , notice: 'Favorite place was successfully added.'
      respond_to do |format|
        format.js { render :js => "my_function();" }
      end

對此

      respond_to do |format|
        format.html { redirect_to favorite_places_path , notice: 'Favorite place was successfully added.' }
        format.js { render :js => "my_function();" }
      end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM