我想从控制器中的一个方法调用一个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");
}应用程序/控制器/收藏夹_位置_控制器
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发布于 2014-11-11 21:45:08
在respond_to块之前有一个redirect_to,这是错误的- redirect_to应该在respond_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();" }
endhttps://stackoverflow.com/questions/26866117
复制相似问题